<!DOCTYPE html>
<html  lang="zh">
<head>
    <meta charset="utf-8">
<title>分类: Spring - 一灰灰Blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />



    <meta name="description" content="好好学习，努力搬砖，Java,Shell,Spring,Mysql,DB,MVC,Servlet,Redis也需了解一二">
<meta property="og:type" content="website">
<meta property="og:title" content="一灰灰Blog">
<meta property="og:url" content="https://blog.hhui.top/hexblog/categories/开源/Spring/index.html">
<meta property="og:site_name" content="一灰灰Blog">
<meta property="og:description" content="好好学习，努力搬砖，Java,Shell,Spring,Mysql,DB,MVC,Servlet,Redis也需了解一二">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://blog.hhui.top/hexblog/images/og_image.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一灰灰Blog">
<meta name="twitter:description" content="好好学习，努力搬砖，Java,Shell,Spring,Mysql,DB,MVC,Servlet,Redis也需了解一二">
<meta name="twitter:image" content="https://blog.hhui.top/hexblog/images/og_image.png">







<link rel="icon" href="/hexblog/images/avatar.jpg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/docco.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/hexblog/css/back-to-top.css">


    
    

    
    
<script>
var _hmt = _hmt || [];
(function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?028d9e53f991d9739ecc7cc42e13c500";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();
</script>

    
    

    
    
<link rel="stylesheet" href="/hexblog/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    

    
    
    


<link rel="stylesheet" href="/hexblog/css/style.css">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/hexblog/">
            
                <img src="/hexblog/images/avatar.jpg" alt="一灰灰Blog" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/hexblog/.">首页</a>
                
                <a class="navbar-item"
                href="/hexblog/archives">归档</a>
                
                <a class="navbar-item"
                href="/hexblog/tags">标签</a>
                
                <a class="navbar-item"
                href="http://spring.hhui.top">Spring</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/Java/">Java</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/Python/">Python</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/DB/">DB</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/Shell/">Shell</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/Quick系列/">Quick系列</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/前端/">前端</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/开源/">开源</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/工具/">工具</a>
                
                <a class="navbar-item"
                href="/hexblog/categories/随笔/">随笔</a>
                
                <a class="navbar-item"
                href="/hexblog/about">关于</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="Download on GitHub" href="https://github.com/liuyueyi">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                
                <a class="navbar-item search" title="搜索" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-7-widescreen has-order-2 column-main"><div class="card">
    <div class="card-content">
        <nav class="breadcrumb" aria-label="breadcrumbs">
        <ul>
            <li><a href="/hexblog/categories">分类</a></li>
            
            <li><a href="/hexblog/categories/开源/">开源</a></li>
            
            <li class="is-active"><a href="#" aria-current="page">Spring</a></li>
        </ul>
        </nav>
    </div>
</div>

    <div class="card">
    
        <a href="/hexblog/2019/04/01/190401-SpringBoot远程debug设置/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                190401-SpringBoot远程debug设置
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-04-01T11:47:46.000Z">2019-04-01</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    1 分钟 读完 (大约 208 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2019/04/01/190401-SpringBoot远程debug设置/">190401-SpringBoot远程debug设置</a>
            
        </h1>
        <div class="content">
            

            <p>记录下SpringBoot jar启动方式，开启远程debug的命令</p>
<figure class="highlight bash hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -server -Xms512m -Xmx512m -XX:AutoBoxCacheMax=20480 -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=9999,<span class="hljs-built_in">suspend</span>=n -jar web.jar</span><br></pre></td></tr></table></figure>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2019/04/01/190401-SpringBoot远程debug设置/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/11/12/181112-springboot应用下线配置/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                181112-springboot应用下线配置
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-11-12T07:24:24.000Z">2018-11-12</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    3 分钟 读完 (大约 380 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/11/12/181112-springboot应用下线配置/">181112-springboot应用下线配置</a>
            
        </h1>
        <div class="content">
            

            <p>本篇记录SpringBoot 2.x借助Actuator实现优雅的下线的配置方式</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/11/12/181112-springboot应用下线配置/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/10/11/181011-SpringCloud之DiscoverClient无法获取Service记录/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                181011-SpringCloud之DiscoverClient无法获取Service记录
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-10-11T11:06:41.000Z">2018-10-11</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/问题记录/">问题记录</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    3 分钟 读完 (大约 423 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/10/11/181011-SpringCloud之DiscoverClient无法获取Service记录/">181011-SpringCloud之DiscoverClient无法获取Service记录</a>
            
        </h1>
        <div class="content">
            

            <p>最终解决问题之后，才发现是自己走进盲角了，花了不少时间，特此记录</p>
<h2 id="I-问题说明"><a href="#I-问题说明" class="headerlink" title="I. 问题说明"></a>I. 问题说明</h2><p>在测试使用SpringCloud全家桶时，服务向注册中心注册，然后client就可以相互之间实现RPC调用（其实还是http访问）</p>
<p>如果我想看看当前注册中心获取了哪些服务，可以怎么办？</p>
<p>一个简单的方法就是借助<code>DiscoverClient</code>来做，然后问题就来了</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Application</span><span class="hljs-params">(DiscoveryClient discoveryClient)</span> </span>&#123;</span><br><span class="line">        List&lt;String&gt; list = discoveryClient.getServices();</span><br><span class="line">        System.out.println(list);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面的代码执行之后，发现返回是空数组；蛋疼的是换另外一个工程，同样的方式，却可以拿到注册的服务名</p>
<p>查了半天，最好才发现拿不到是因为在配置中添加了</p>
<figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">eureka.client.fetchRegistry=false</span><br></pre></td></tr></table></figure>
<p>上面就表示不会去主动获取注册的服务，因此也就拿不到服务了；改成true之后就ok了</p>
<h2 id="II-其他"><a href="#II-其他" class="headerlink" title="II. 其他"></a>II. 其他</h2><h3 id="1-一灰灰Blog：-https-liuyueyi-github-io-hexblog"><a href="#1-一灰灰Blog：-https-liuyueyi-github-io-hexblog" class="headerlink" title="1. 一灰灰Blog： https://liuyueyi.github.io/hexblog"></a>1. <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a>： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">https://liuyueyi.github.io/hexblog</a></h3><p>一灰灰的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="2-声明"><a href="#2-声明" class="headerlink" title="2. 声明"></a>2. 声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因个人能力有限，难免有疏漏和错误之处，如发现bug或者有更好的建议，欢迎批评指正，不吝感激</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="3-扫描关注"><a href="#3-扫描关注" class="headerlink" title="3. 扫描关注"></a>3. 扫描关注</h3><p><strong>一灰灰blog</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>
<p><strong>知识星球</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/goals.png" alt="goals"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/09/05/180905-Spring返回Json对象/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180905-Spring返回Json对象
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-09-05T14:12:28.000Z">2018-09-05</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    3 分钟 读完 (大约 472 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/09/05/180905-Spring返回Json对象/">180905-Spring返回Json对象</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之返回Json串"><a href="#Spring之返回Json串" class="headerlink" title="Spring之返回Json串"></a>Spring之返回Json串</h1><p>使用Spring搭建的web后端服务，与前端通过Json串进行交互，特此记录下使用姿势</p>
<h2 id="1-直接返回JsonString"><a href="#1-直接返回JsonString" class="headerlink" title="1. 直接返回JsonString"></a>1. 直接返回JsonString</h2><p>这个可以说是最简单和最常见的一种使用姿势了，直接返回String，如下</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@ResponseBody</span></span><br><span class="line"><span class="hljs-meta">@RequestMapping</span>(value = <span class="hljs-string">"/body"</span>)</span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">body</span><span class="hljs-params">(@RequestBody Req req)</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">return</span> JSON.toJSONString(req);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-使用JsonInclude注解"><a href="#2-使用JsonInclude注解" class="headerlink" title="2. 使用JsonInclude注解"></a>2. 使用<code>JsonInclude</code>注解</h2><p>直接返回的是一个对象，然后交给框架来将对象转换为String丢给前端</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RequestMapping</span>(path = &#123;<span class="hljs-string">"/index"</span>, <span class="hljs-string">"/"</span>&#125;)</span><br><span class="line"><span class="hljs-keyword">public</span> ResponseWrapper&lt;List&lt;PoetryDTO&gt;&gt; index() &#123;</span><br><span class="line">    List&lt;PoetryDTO&gt; ans = poetryReadService.getIndex();</span><br><span class="line">    <span class="hljs-keyword">return</span> ResponseWrapper.successReturn(ans);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>直接用上面的方式时，可能会抛出，提示没有对应的<code>HttpMessageConverter</code>来转换ResponseWrapper对象</p>
<p>一个简单的使用姿势就是直接使用注解 <code>com.fasterxml.jackson.annotation.JsonInclude</code></p>
<p>如下即可</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">JsonInclude</span><br><span class="line"><span class="hljs-meta">@Data</span></span><br><span class="line"><span class="hljs-meta">@NoArgsConstructor</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ResponseWrapper</span>&lt;<span class="hljs-title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> code;</span><br><span class="line">    <span class="hljs-keyword">private</span> String msg;</span><br><span class="line">    <span class="hljs-keyword">private</span> T data;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ResponseWrapper</span><span class="hljs-params">(<span class="hljs-keyword">int</span> code, String msg, T data)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">this</span>.code = code;</span><br><span class="line">        <span class="hljs-keyword">this</span>.msg = msg;</span><br><span class="line">        <span class="hljs-keyword">this</span>.data = data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function">ResponseWrapper&lt;T&gt; <span class="hljs-title">buildSuccess</span><span class="hljs-params">(T data)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ResponseWrapper&lt;&gt;(<span class="hljs-number">200</span>, <span class="hljs-string">"success"</span>, data);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function">ResponseWrapper&lt;T&gt; <span class="hljs-title">buildError</span><span class="hljs-params">(<span class="hljs-keyword">int</span> code, String msg, T data)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ResponseWrapper&lt;&gt;(code, msg, data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因此关键就是<code>HttpMessageConverter</code>在起作用了，下一篇重点关注下这个是什么东西，干嘛用，以及如何自己实现一个特定需求的转换器</p>
<h2 id="II-其他"><a href="#II-其他" class="headerlink" title="II. 其他"></a>II. 其他</h2><h3 id="1-一灰灰Blog：-https-liuyueyi-github-io-hexblog"><a href="#1-一灰灰Blog：-https-liuyueyi-github-io-hexblog" class="headerlink" title="1. 一灰灰Blog： https://liuyueyi.github.io/hexblog"></a>1. <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a>： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">https://liuyueyi.github.io/hexblog</a></h3><p>一灰灰的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="2-声明"><a href="#2-声明" class="headerlink" title="2. 声明"></a>2. 声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因个人能力有限，难免有疏漏和错误之处，如发现bug或者有更好的建议，欢迎批评指正，不吝感激</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="3-扫描关注"><a href="#3-扫描关注" class="headerlink" title="3. 扫描关注"></a>3. 扫描关注</h3><p><strong>一灰灰blog</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>
<p><strong>知识星球</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/goals.png" alt="goals"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/30/180830-SpringBoot之获取application-yml配置参数/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180830-SpringBoot之获取application.yml配置参数
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-30T15:25:38.000Z">2018-08-30</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    6 分钟 读完 (大约 853 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/30/180830-SpringBoot之获取application-yml配置参数/">180830-SpringBoot之获取application.yml配置参数</a>
            
        </h1>
        <div class="content">
            

            <h1 id="SpringBoot之获取Application-yml配置参数"><a href="#SpringBoot之获取Application-yml配置参数" class="headerlink" title="SpringBoot之获取Application.yml配置参数"></a>SpringBoot之获取Application.yml配置参数</h1><p>需要获取配置文件中的配置参数的场景挺多的，常见的一种方式就是直接从<code>Enironment</code>对象中获取，或者使用 <code>@Value</code> 注解的方式注入，但是这都有一个前提，需要确切的知道配置的name</p>
<p>如果某些场景下，我需要遍历配置参数可以怎么办？</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/08/30/180830-SpringBoot之获取application-yml配置参数/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/15/180815-Spring之RestTemplate使用小结二：中级使用篇/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180815-Spring之RestTemplate使用小结二：中级使用篇
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-15T11:46:55.000Z">2018-08-15</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    20 分钟 读完 (大约 3039 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/15/180815-Spring之RestTemplate使用小结二：中级使用篇/">180815-Spring之RestTemplate使用小结二：中级使用篇</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之RestTemplate中级使用篇"><a href="#Spring之RestTemplate中级使用篇" class="headerlink" title="Spring之RestTemplate中级使用篇"></a>Spring之RestTemplate中级使用篇</h1><p>前面一篇介绍了如何使用<code>RestTemplate</code>发起post和get请求，然而也只能满足一些基本的场景，对于一些特殊的如需要设置请求头，添加认证信息等场景，却没有提及可以怎么做，这一篇则相当于进阶版，将主要介绍</p>
<ul>
<li>get/post请求如何携带 header</li>
<li>post传文件可以怎么玩, post提交json串怎么处理</li>
<li><code>exchange</code>方法的使用姿势</li>
</ul>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/08/15/180815-Spring之RestTemplate使用小结二：中级使用篇/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/13/180813-Spring之RestTemplate使用小结一/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180813-Spring之RestTemplate使用小结一
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-13T12:45:22.000Z">2018-08-13</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    23 分钟 读完 (大约 3391 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/13/180813-Spring之RestTemplate使用小结一/">180813-Spring之RestTemplate使用小结一</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之RestTemplate初级使用篇"><a href="#Spring之RestTemplate初级使用篇" class="headerlink" title="Spring之RestTemplate初级使用篇"></a>Spring之RestTemplate初级使用篇</h1><p>作为一个Java后端，需要通过HTTP请求其他的网络资源可以说是一个比较常见的case了；一般怎么做呢？</p>
<p>可能大部分的小伙伴直接捞起Apache的HttpClient开始做，或者用其他的一些知名的开源库如OkHttp, 当然原生的HttpURLConnection也是没问题的</p>
<p>本篇博文则主要关注点放在Spring的生态下，利用<code>RestTemplate</code>来发起Http请求的使用姿势</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/08/13/180813-Spring之RestTemplate使用小结一/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/04/180804-Spring之动态注册bean/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180804-Spring之动态注册bean
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-04T10:52:26.000Z">2018-08-04</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    4 分钟 读完 (大约 576 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/04/180804-Spring之动态注册bean/">180804-Spring之动态注册bean</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之动态注册bean"><a href="#Spring之动态注册bean" class="headerlink" title="Spring之动态注册bean"></a>Spring之动态注册bean</h1><p>什么场景下，需要主动向Spring容器注册bean呢？</p>
<p>如我之前做个的一个支持扫表的基础平台，使用者只需要添加基础配置 + Groovy任务，就可以丢到这个平台上面来运行了，而这个基础平台是一直都在运行的，所以在新来任务时，最直观需要注册的就是 <code>DataSource</code> 数据源这个bean了，那么可以怎么玩？</p>
<h2 id="I-主动注册Bean支持"><a href="#I-主动注册Bean支持" class="headerlink" title="I. 主动注册Bean支持"></a>I. 主动注册Bean支持</h2><p>借助<code>BeanDefinition</code>来实现bean的定义，从最终的使用来看，代码比较少，几行而已</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-function">T <span class="hljs-title">registerBean</span><span class="hljs-params">(String name, Class&lt;T&gt; clazz, Object... args)</span> </span>&#123;</span><br><span class="line">      BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);</span><br><span class="line">      <span class="hljs-keyword">if</span> (args.length &gt; <span class="hljs-number">0</span>) &#123;</span><br><span class="line">          <span class="hljs-keyword">for</span> (Object arg : args) &#123;</span><br><span class="line">              beanDefinitionBuilder.addConstructorArgValue(arg);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();</span><br><span class="line">  </span><br><span class="line">      BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();</span><br><span class="line">      beanFactory.registerBeanDefinition(name, beanDefinition);</span><br><span class="line">      <span class="hljs-keyword">return</span> applicationContext.getBean(name, clazz);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试如下</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">import</span> com.github.hui.story.quickstory.server.VisitService;</span><br><span class="line"><span class="hljs-keyword">import</span> lombok.ToString;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.beans.factory.config.BeanDefinition;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.beans.factory.support.BeanDefinitionBuilder;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.beans.factory.support.BeanDefinitionRegistry;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.context.ConfigurableApplicationContext;</span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">@Component</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BeanHolder</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> ConfigurableApplicationContext applicationContext;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BeanHolder</span><span class="hljs-params">(ConfigurableApplicationContext applicationContext)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">this</span>.applicationContext = applicationContext;</span><br><span class="line">        initSer();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initSer</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        InrSer ser = registerBean(<span class="hljs-string">"test"</span>, InrSer.class);</span><br><span class="line">        ser.name = <span class="hljs-string">"一灰"</span>;</span><br><span class="line">        ser.uid = <span class="hljs-number">22</span>;</span><br><span class="line">        System.out.println(ser);</span><br><span class="line"></span><br><span class="line">        InrSer ser2 = registerBean(<span class="hljs-string">"test2"</span>, InrSer.class, <span class="hljs-string">"一灰灰Blog"</span>, <span class="hljs-number">20</span>);</span><br><span class="line">        System.out.println(ser2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@ToString</span></span><br><span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InrSer</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">private</span> String name;</span><br><span class="line">        <span class="hljs-keyword">private</span> Integer uid;</span><br><span class="line"></span><br><span class="line">        <span class="hljs-meta">@Autowired</span></span><br><span class="line">        <span class="hljs-keyword">private</span> VisitService visitService;</span><br><span class="line"></span><br><span class="line">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">InrSer</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">InrSer</span><span class="hljs-params">(String name, Integer uid)</span> </span>&#123;</span><br><span class="line">            <span class="hljs-keyword">this</span>.name = name;</span><br><span class="line">            <span class="hljs-keyword">this</span>.uid = uid;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> &lt;T&gt; <span class="hljs-function">T <span class="hljs-title">registerBean</span><span class="hljs-params">(String name, Class&lt;T&gt; clazz, Object... args)</span> </span>&#123;</span><br><span class="line">        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);</span><br><span class="line">        <span class="hljs-keyword">if</span> (args.length &gt; <span class="hljs-number">0</span>) &#123;</span><br><span class="line">            <span class="hljs-keyword">for</span> (Object arg : args) &#123;</span><br><span class="line">                beanDefinitionBuilder.addConstructorArgValue(arg);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();</span><br><span class="line"></span><br><span class="line">        BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();</span><br><span class="line">        beanFactory.registerBeanDefinition(name, beanDefinition);</span><br><span class="line">        <span class="hljs-keyword">return</span> applicationContext.getBean(name, clazz);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出如下</p>
<p><img src="https://upload-images.jianshu.io/upload_images/1405936-8a8e6b58a93e2e27.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="show"></p>
<h2 id="II-其他"><a href="#II-其他" class="headerlink" title="II. 其他"></a>II. 其他</h2><h3 id="1-一灰灰Blog：-https-liuyueyi-github-io-hexblog"><a href="#1-一灰灰Blog：-https-liuyueyi-github-io-hexblog" class="headerlink" title="1. 一灰灰Blog： https://liuyueyi.github.io/hexblog"></a>1. <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a>： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">https://liuyueyi.github.io/hexblog</a></h3><p>一灰灰的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="2-声明"><a href="#2-声明" class="headerlink" title="2. 声明"></a>2. 声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因个人能力有限，难免有疏漏和错误之处，如发现bug或者有更好的建议，欢迎批评指正，不吝感激</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="3-扫描关注"><a href="#3-扫描关注" class="headerlink" title="3. 扫描关注"></a>3. 扫描关注</h3><p><strong>一灰灰blog</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>
<p><strong>知识星球</strong></p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/goals.png" alt="goals"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/03/180803-Spring定时任务高级使用篇/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180803-Spring定时任务高级使用篇
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-03T11:51:38.000Z">2018-08-03</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    13 分钟 读完 (大约 2022 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/03/180803-Spring定时任务高级使用篇/">180803-Spring定时任务高级使用篇</a>
            
        </h1>
        <div class="content">
            

            <p><img src="https://upload-images.jianshu.io/upload_images/1405936-c32b2855570fce76.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="logo"></p>
<p>前面一篇博文 <a href="https://liuyueyi.github.io/hexblog/2018/08/01/180801-Spring%E4%B9%8B%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E7%AF%87/" target="_blank" rel="noopener">《Spring之定时任务基本使用篇》</a> 介绍了Spring环境下，定时任务的简单使用姿势，也留了一些问题，这一篇则希望能针对这些问题给个答案</p>
<h2 id="I-定时任务进阶篇"><a href="#I-定时任务进阶篇" class="headerlink" title="I. 定时任务进阶篇"></a>I. 定时任务进阶篇</h2><h3 id="1-问题小结"><a href="#1-问题小结" class="headerlink" title="1. 问题小结"></a>1. 问题小结</h3><p>前面一篇博文，抛出了下面的几个问题，接下来则围绕问题进行分析</p>
<ul>
<li>一个项目中有多个定时任务时，他们是并行执行的还是串行执行的？</li>
<li>如果默认是串行的<ul>
<li>那么有相同的crond表达式的定时任务之间，有先后顺序么？</li>
<li>某个任务的阻塞是否会影响后面的任务？</li>
<li>如果需要他们并行执行，可以怎么做？</li>
</ul>
</li>
<li>如果是并发执行的<ul>
<li>是新创建线程还是采用线程池来复用呢？</li>
<li>在并发执行时，假设有个每秒执行一次的任务，但是它执行一次消耗的时间大于1s时，这个任务的表现时怎样的呢？不断地新增线程来执行还是等执行完毕之后再执行下一次的呢?</li>
</ul>
</li>
</ul>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/08/03/180803-Spring定时任务高级使用篇/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/08/01/180801-Spring之定时任务基本使用篇/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180801-Spring之定时任务基本使用篇
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-08-01T13:59:42.000Z">2018-08-01</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    10 分钟 读完 (大约 1468 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/08/01/180801-Spring之定时任务基本使用篇/">180801-Spring之定时任务基本使用篇</a>
            
        </h1>
        <div class="content">
            

            <p><img src="https://upload-images.jianshu.io/upload_images/1405936-d1225b77087dad92.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h1 id="Spring之定时任务基本使用篇"><a href="#Spring之定时任务基本使用篇" class="headerlink" title="Spring之定时任务基本使用篇"></a>Spring之定时任务基本使用篇</h1><p>spring-boot项目中，想添加一个定时任务，可以怎么办？</p>
<ul>
<li>不管什么项目，都是可以直接用JDK原生的定时任务来实现</li>
<li>借助<code>@Scheduled</code>注解来使用</li>
</ul>
<p>本篇博文则主要集中在在SpringBoot项目中，怎么使用定时任务</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/08/01/180801-Spring之定时任务基本使用篇/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/07/30/180730-Spring之RequestBody的使用姿势小结/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180730-Spring之RequestBody的使用姿势小结
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-07-30T11:36:18.000Z">2018-07-30</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    18 分钟 读完 (大约 2710 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/07/30/180730-Spring之RequestBody的使用姿势小结/">180730-Spring之RequestBody的使用姿势小结</a>
            
        </h1>
        <div class="content">
            

            <p><img src="https://upload-images.jianshu.io/upload_images/1405936-84708f95ebf181e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="logo"></p>
<h1 id="Spring之RequestBody的使用姿势小结"><a href="#Spring之RequestBody的使用姿势小结" class="headerlink" title="Spring之RequestBody的使用姿势小结"></a>Spring之RequestBody的使用姿势小结</h1><p>SpringMVC中处理请求参数有好几种不同的方式，如我们常见的下面几种</p>
<ul>
<li>根据 <code>HttpServletRequest</code> 对象获取</li>
<li>根据 <code>@PathVariable</code> 注解获取url参数</li>
<li>根据 <code>@RequestParam</code> 注解获取请求参数</li>
<li>根据Bean的方式获取请求参数</li>
<li>根据 <code>@ModelAttribute</code> 注解获取请求参数</li>
</ul>
<p>对上面几种方式有兴趣的可以看一下这篇博文: <a href="https://blog.hhui.top/hexblog/2018/01/04/SpringMVC%E4%B9%8B%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E8%8E%B7%E5%8F%96%E6%96%B9%E5%BC%8F/">SpringMVC之请求参数的获取方式</a></p>
<p>除了上面的几种方式之外，还有一种 <code>@RequestBody</code> 的使用方式，本文则主要介绍这种传参的使用姿势和相关注意事项</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/07/30/180730-Spring之RequestBody的使用姿势小结/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/07/13/180713-Spring之借助Redis设计访问计数器之扩展篇/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180713-Spring之借助Redis设计访问计数器之扩展篇
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-07-13T14:53:29.000Z">2018-07-13</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/实战系列/">实战系列</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    13 分钟 读完 (大约 1933 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/07/13/180713-Spring之借助Redis设计访问计数器之扩展篇/">180713-Spring之借助Redis设计访问计数器之扩展篇</a>
            
        </h1>
        <div class="content">
            

            <p><img src="https://upload-images.jianshu.io/upload_images/1405936-bc88f4896a16a5fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="logo"></p>
<p>之前写了一篇博文，简单的介绍了下如何利用Redis配合Spring搭建一个web的访问计数器，之前的内容比较初级，现在考虑对其进行扩展，新增访问者记录</p>
<ul>
<li>记录当前站点的总访问人数（根据Ip或则设备号）</li>
<li>记录当前访问者在总访问人数中的排名</li>
<li>记录每个子页面的访问计数，记录站点的总访问计数</li>
</ul>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/07/13/180713-Spring之借助Redis设计访问计数器之扩展篇/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/06/26/180626-Spring之借助Redis设计一个简单访问计数器/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180626-Spring之借助Redis设计一个简单访问计数器
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-06-26T14:21:28.000Z">2018-06-26</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/实战系列/">实战系列</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    10 分钟 读完 (大约 1436 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/06/26/180626-Spring之借助Redis设计一个简单访问计数器/">180626-Spring之借助Redis设计一个简单访问计数器</a>
            
        </h1>
        <div class="content">
            

            <p><img src="https://upload-images.jianshu.io/upload_images/1405936-2a92a5f77c5d686f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="logo"></p>
<h1 id="Spring之借助Redis设计一个简单访问计数器"><a href="#Spring之借助Redis设计一个简单访问计数器" class="headerlink" title="Spring之借助Redis设计一个简单访问计数器"></a>Spring之借助Redis设计一个简单访问计数器</h1><p>为什么要做一个访问计数？之前的个人博客用得是卜算子做站点访问计数，用起来挺好，但出现较多次的响应很慢，再其次就是个人博客实在是访问太少，数据不好看😢…</p>
<p>前面一篇博文简单介绍了Spring中的RedisTemplate的配置与使用，那么这篇算是一个简单的应用case了,主要基于Redis的计数器来实现统计</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/06/26/180626-Spring之借助Redis设计一个简单访问计数器/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/06/23/180623-SpringBoot之logback配置文件/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180623-SpringBoot之logback配置文件
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-06-23T10:57:29.000Z">2018-06-23</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    8 分钟 读完 (大约 1268 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/06/23/180623-SpringBoot之logback配置文件/">180623-SpringBoot之logback配置文件</a>
            
        </h1>
        <div class="content">
            

            <h1 id="SpringBoot配置logback"><a href="#SpringBoot配置logback" class="headerlink" title="SpringBoot配置logback"></a>SpringBoot配置logback</h1><p>项目的日志配置属于比较常见的case了，之前接触和使用的都是Spring结合xml的方式，引入几个依赖，然后写个 <code>logback.xml</code> 配置文件即可，那么在SpringBoot中可以怎么做？</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/06/23/180623-SpringBoot之logback配置文件/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/06/12/180612-Spring之Yml配置文件加载问题/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180612-Spring之Yml配置文件加载问题
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-06-12T11:20:33.000Z">2018-06-12</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    2 分钟 读完 (大约 362 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/06/12/180612-Spring之Yml配置文件加载问题/">180612-Spring之Yml配置文件加载问题</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Yml配置文件加载问题"><a href="#Yml配置文件加载问题" class="headerlink" title="Yml配置文件加载问题"></a>Yml配置文件加载问题</h1><p>在resource目录下有一个application.yml文件，希望是通过<code>@PropertySource</code>注解，将配置文件数据读取到<code>Environment</code>中，然而调试发现数据始终读取不到，google之后，记录下解决方法</p>
<p>在测试用例中，指定初始化方式 <code>@ContextConfiguration(classes = RedisConf.class, initializers = ConfigFileApplicationContextInitializer.class)</code></p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(classes = RedisConf.class, initializers = ConfigFileApplicationContextInitializer.class)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedisTest</span> </span>&#123;</span><br><span class="line">    <span class="hljs-meta">@Test</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testRedis</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        String ans = JedisClient.getStr(<span class="hljs-string">"hello"</span>);</span><br><span class="line">        System.out.println(ans);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>对应的配置类</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Configuration</span></span><br><span class="line"><span class="hljs-meta">@PropertySource</span>(value = <span class="hljs-string">"classpath:application.yml"</span>)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedisConf</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> Environment environment;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> RedisTemplate&lt;String, String&gt; <span class="hljs-title">redisTemplate</span><span class="hljs-params">(RedisConnectionFactory redisConnectionFactory)</span> </span>&#123;</span><br><span class="line">        RedisTemplate&lt;String, String&gt; redisTemplate = <span class="hljs-keyword">new</span> RedisTemplate&lt;&gt;();</span><br><span class="line">        redisTemplate.setConnectionFactory(redisConnectionFactory);</span><br><span class="line"></span><br><span class="line">        DefaultStrSerializer serializer = <span class="hljs-keyword">new</span> DefaultStrSerializer();</span><br><span class="line">        redisTemplate.setValueSerializer(serializer);</span><br><span class="line">        redisTemplate.setHashValueSerializer(serializer);</span><br><span class="line">        redisTemplate.setKeySerializer(serializer);</span><br><span class="line">        redisTemplate.setHashKeySerializer(serializer);</span><br><span class="line"></span><br><span class="line">        redisTemplate.afterPropertiesSet();</span><br><span class="line"></span><br><span class="line">        JedisClient.register(redisTemplate);</span><br><span class="line">        <span class="hljs-keyword">return</span> redisTemplate;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Bean</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> RedisConnectionFactory <span class="hljs-title">redisConnectionFactory</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        LettuceConnectionFactory fac = <span class="hljs-keyword">new</span> LettuceConnectionFactory();</span><br><span class="line">        fac.getStandaloneConfiguration().setHostName(environment.getProperty(<span class="hljs-string">"spring.redis.host"</span>));</span><br><span class="line">        fac.getStandaloneConfiguration().setPort(Integer.parseInt(environment.getProperty(<span class="hljs-string">"spring.redis.port"</span>)));</span><br><span class="line">        fac.getStandaloneConfiguration()</span><br><span class="line">                .setPassword(RedisPassword.of(environment.getProperty(<span class="hljs-string">"spring.redis.password"</span>)));</span><br><span class="line">        fac.afterPropertiesSet();</span><br><span class="line">        <span class="hljs-keyword">return</span> fac;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="II-其他"><a href="#II-其他" class="headerlink" title="II. 其他"></a>II. 其他</h2><h3 id="1-一灰灰Blog：-https-liuyueyi-github-io-hexblog"><a href="#1-一灰灰Blog：-https-liuyueyi-github-io-hexblog" class="headerlink" title="1. 一灰灰Blog： https://liuyueyi.github.io/hexblog"></a>1. <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a>： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">https://liuyueyi.github.io/hexblog</a></h3><p>一灰灰的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="2-声明"><a href="#2-声明" class="headerlink" title="2. 声明"></a>2. 声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因个人能力有限，难免有疏漏和错误之处，如发现bug或者有更好的建议，欢迎批评指正，不吝感激</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="3-扫描关注"><a href="#3-扫描关注" class="headerlink" title="3. 扫描关注"></a>3. 扫描关注</h3><p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/06/11/180611-Spring之RedisTemplate配置与使用/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180611-Spring之RedisTemplate配置与使用
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-06-11T10:19:06.000Z">2018-06-11</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    13 分钟 读完 (大约 1966 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/06/11/180611-Spring之RedisTemplate配置与使用/">180611-Spring之RedisTemplate配置与使用</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之RedisTemplate配置与使用"><a href="#Spring之RedisTemplate配置与使用" class="headerlink" title="Spring之RedisTemplate配置与使用"></a>Spring之RedisTemplate配置与使用</h1><p>Spring针对Redis的使用，封装了一个比较强大的Template以方便使用；之前在Spring的生态圈中也使用过redis，但直接使用Jedis进行相应的交互操作，现在正好来看一下RedisTemplate是怎么实现的，以及使用起来是否更加便利</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/06/11/180611-Spring之RedisTemplate配置与使用/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/06/09/180609-Spring之事件驱动机制的简单使用/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180609-Spring之事件驱动机制的简单使用
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-06-09T11:37:20.000Z">2018-06-09</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    7 分钟 读完 (大约 1032 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/06/09/180609-Spring之事件驱动机制的简单使用/">180609-Spring之事件驱动机制的简单使用</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring之事件驱动机制的简单使用"><a href="#Spring之事件驱动机制的简单使用" class="headerlink" title="Spring之事件驱动机制的简单使用"></a>Spring之事件驱动机制的简单使用</h1><p>关于事件的发起与相应，在客户端的交互中可算是非常频繁的事情了，关于事件的发布订阅，在Java生态中，EventBus可谓是非常有名了，而Spring也提供了事件机制，本文则主要介绍后端如何在Spring的环境中，使用事件机制</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/06/09/180609-Spring之事件驱动机制的简单使用/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/05/31/180531-Spring中JavaConfig知识小结/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                180531-Spring中JavaConfig知识小结
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-05-31T11:52:41.000Z">2018-05-31</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    13 分钟 读完 (大约 1929 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/05/31/180531-Spring中JavaConfig知识小结/">180531-Spring中JavaConfig知识小结</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Sring中JavaConfig使用姿势"><a href="#Sring中JavaConfig使用姿势" class="headerlink" title="Sring中JavaConfig使用姿势"></a>Sring中JavaConfig使用姿势</h1><p>去掉xml的配置方式，改成用Java来配置，最常见的就是将xml中的 bean定义， scanner包扫描，属性文件的配置信息读取等</p>
<h2 id="I-几个基本注解"><a href="#I-几个基本注解" class="headerlink" title="I. 几个基本注解"></a>I. 几个基本注解</h2><h3 id="1-Configuration注解"><a href="#1-Configuration注解" class="headerlink" title="1. Configuration注解"></a>1. Configuration注解</h3><p>在javaConfig中注解<code>@Configuration</code>用来代替一个xml文件，可以简单的理解他们的作用是相等的，一般bean的定义也都是放在被这个注解修饰的类中</p>
<p>如一个基本的配置文件如下</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Configuration</span></span><br><span class="line"><span class="hljs-meta">@ComponentScan</span>(<span class="hljs-string">"com.git.hui.rabbit.spring"</span>)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringConfig</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> Environment environment;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setEnvironment</span><span class="hljs-params">(Environment environment)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">this</span>.environment = environment;</span><br><span class="line">        System.out.println(<span class="hljs-string">"then env: "</span> + environment);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Bean</span>(name=<span class="hljs-string">"connectionFactory"</span>)</span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> ConnectionFactory <span class="hljs-title">connectionFactory</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        CachingConnectionFactory factory = <span class="hljs-keyword">new</span> CachingConnectionFactory();</span><br><span class="line">        factory.setHost(<span class="hljs-string">"127.0.0.1"</span>);</span><br><span class="line">        factory.setPort(<span class="hljs-number">5672</span>);</span><br><span class="line">        factory.setUsername(<span class="hljs-string">"admin"</span>);</span><br><span class="line">        factory.setPassword(<span class="hljs-string">"admin"</span>);</span><br><span class="line">        factory.setVirtualHost(<span class="hljs-string">"/"</span>);</span><br><span class="line">        <span class="hljs-keyword">return</span> factory;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Bean</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> RabbitAdmin <span class="hljs-title">rabbitAdmin</span><span class="hljs-params">(ConnectionFactory connectionFactory)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> RabbitAdmin(connectionFactory);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-Bean-注解"><a href="#2-Bean-注解" class="headerlink" title="2. Bean 注解"></a>2. Bean 注解</h3><p>上面的例子中，在方法上添加了@Bean注解，这个就相当于传统的</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"rabbitAdmin"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.amqp.rabbit.core.RabbitAdmin"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>因此在需要引入rabbitAdmin实例的地方，可以如下使用</p>
<p>a. 属性字段上添加 <code>@Autowired</code>注解</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RConsumer</span> </span>&#123;</span><br><span class="line">  <span class="hljs-meta">@Autowired</span></span><br><span class="line">  <span class="hljs-keyword">private</span> RabbitAdmin rabbitAdmin;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>b. 设置方法上添加 <code>@Autowired</code>注解</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RConsumer</span> </span>&#123;</span><br><span class="line">  <span class="hljs-keyword">private</span> RabbitAdmin rabbitAdmin;</span><br><span class="line">  </span><br><span class="line">  <span class="hljs-meta">@Autowired</span></span><br><span class="line">  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setRabbitAdmin</span><span class="hljs-params">(RabbitAdmin rabbitAdmin)</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">this</span>.rabbitAdmin = rabbitAdmin;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>c. 使用构造器的方式</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RConsumer</span> </span>&#123;</span><br><span class="line">  <span class="hljs-keyword">private</span> RabbitAdmin rabbitAdmin;</span><br><span class="line">  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">RConsumer</span><span class="hljs-params">(RabbitAdmin rabbitAdmin)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">this</span>.rabbitAdmin = rabbitAdmin;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面就是Spring容器支持的几种典型的IoC方式</p>
<h3 id="3-ComponentScan"><a href="#3-ComponentScan" class="headerlink" title="3. ComponentScan"></a>3. ComponentScan</h3><p>这个类似于xml中的 <code>&lt;context:component-scan&quot;/&gt;</code> 标签</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@ComponentScan</span>(<span class="hljs-string">"com.git.hui.rabbit.spring"</span>)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringConfig</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面的这个配置，表示自动扫描包 <code>com.git.hui.rabbit.spring</code> 下面的bean （要求类上添加了 @Component, @Repository, @Service）</p>
<p><strong>那么一个问题来了，如果一个类既被自动扫描加载，又显示定义了bean，会怎样?</strong></p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">package</span> com.git.hui.rabbit.spring;</span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="hljs-keyword">import</span> java.util.concurrent.atomic.AtomicInteger;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">@Component</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestBean</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> AtomicInteger count = <span class="hljs-keyword">new</span> AtomicInteger(<span class="hljs-number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">TestBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="hljs-string">"testBean count: "</span> + count.getAndAdd(<span class="hljs-number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>对应的JavaConfig</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Configuration</span></span><br><span class="line"><span class="hljs-meta">@ComponentScan</span>(<span class="hljs-string">"com.git.hui.rabbit.spring"</span>)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringConfig</span> </span>&#123;</span><br><span class="line">    <span class="hljs-meta">@Bean</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> TestBean <span class="hljs-title">testBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> TestBean();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>实际测试，发现这个bean只会有一个实例，即输出计数只会有一条，实际查看<code>ApplicationContext</code>中的内容，TestBean的实例，也确实只有一个，如果改成下面这种场景呢</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Bean</span>(name=<span class="hljs-string">"testBean2"</span>)</span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> TestBean <span class="hljs-title">testBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> TestBean();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>会有两条记录输出，实际查看容器中的Bean对象,会有两个实例如下</p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/blog/daywork/180531_JavaConfig01.jpg" alt="image1"></p>
<p>这和我们的预期也是一样的，因为一个类我可能需要多个不同的Bean实例来干一些事情</p>
<p><strong>那么出现这种JavaConfig定义的beanName与自动扫描的冲突的情况会怎样呢？</strong></p>
<p>新增一个NewBean对象，</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewBean</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> AtomicInteger count = <span class="hljs-keyword">new</span> AtomicInteger(<span class="hljs-number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">NewBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="hljs-string">" newbean count: "</span> + count.getAndAdd(<span class="hljs-number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在JavaConfig中新加一个bean定义，但是BeanName与自动扫描的TestBean重复了</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Bean</span>(name=<span class="hljs-string">"testBean"</span>)</span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> NewBean <span class="hljs-title">newBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> NewBean();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时发现有意思的事情了，从Spring容器中，将查不到TestBean的实例，但是可以查到NewBean的实例</p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/blog/daywork/180531_JavaConfig02.jpg" alt="image1"></p>
<p>这个的表现是：</p>
<ul>
<li>当beanName出现冲突时，JavaConfig的优先级会高于自动加载的，导致自动加载的Bean不会被加载到容器内</li>
</ul>
<p><strong>那么跟着来的一个问题就是如果JavaConfig中定义了两个相同的BeanName的bean呢?</strong></p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Bean</span>(name = <span class="hljs-string">"testBean2"</span>)</span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> NewBean <span class="hljs-title">newBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> NewBean();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">@Bean</span>(name = <span class="hljs-string">"testBean2"</span>)</span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> TestBean <span class="hljs-title">testBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> TestBean();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因为我们TestBean上加了<code>@Component</code>注解,因此容器中至少有一个，但是否会有testBean2这个实例呢？ 通过实际查看是没有的，testBean2这个名被 <code>NewBean</code> 占领了</p>
<p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/blog/daywork/180531_JavaConfig03.jpg" alt="image1"></p>
<p>so，表现上看，加上实测，将上面的定义换个位置，得出下面的结论</p>
<ul>
<li>当出现beanName重名时，先定义的Bean占优</li>
</ul>
<p><strong>然后就是最后一个问题了，当自动扫描时，两个类包不同，但是类名相同，会怎样？</strong></p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">package</span> com.git.hui.rabbit.spring.demo;</span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="hljs-keyword">import</span> java.util.concurrent.atomic.AtomicInteger;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">@Component</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestBean</span> </span>&#123;</span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> AtomicInteger count = <span class="hljs-keyword">new</span> AtomicInteger(<span class="hljs-number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">TestBean</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="hljs-string">" demo.TestBean count: "</span> + count.getAndAdd(<span class="hljs-number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>实测，会抛出一个异常，在使用xml的配置方式时，经常见到的一个BeanName冲突的异常</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name <span class="hljs-string">'testBean'</span> <span class="hljs-keyword">for</span> bean class [com.git.hui.rabbit.spring.demo.TestBean] conflicts with existing, non-compatible bean definition of same name and class [com.git.hui.rabbit.spring.TestBean]</span><br></pre></td></tr></table></figure>
<p><strong>小结：</strong></p>
<ul>
<li>JavaConfig 定义的BeanName与自动扫描的BeanName冲突时，JavaConfig的定义的会被实例化</li>
<li>JavaConfig 中定义了BeanName相同的Bean时，优先定义的有效（这里不抛异常不太能理解）</li>
<li>自动扫描的Bean，不支持类名相同，但是包路径不同的场景（会抛异常）</li>
</ul>
<h3 id="4-Import"><a href="#4-Import" class="headerlink" title="4. Import"></a>4. Import</h3><p>在xml配置中，另一个常见的case就是引入另一个xml配置，在JavaConfig中代替的就是Import注解</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Configuration</span></span><br><span class="line"><span class="hljs-meta">@ComponentScan</span>(<span class="hljs-string">"com.git.hui.rabbit.spring"</span>)</span><br><span class="line"><span class="hljs-meta">@Import</span>(&#123;DirectConsumerConfig.class, FanoutConsumerConfig.class, TopicConsumerConfig.class&#125;)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringConfig</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个就等同于xml中常见的：</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">import</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">"service.xml"</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="II-实例测试"><a href="#II-实例测试" class="headerlink" title="II. 实例测试"></a>II. 实例测试</h2><h3 id="1-xml单测姿势"><a href="#1-xml单测姿势" class="headerlink" title="1. xml单测姿势"></a>1. xml单测姿势</h3><p>上面说了用JavaConfig代替xml配置的方式，另一个关键的地方就是测试用例的写法了，对于之前的xml，有两种常见的使用姿势</p>
<p><strong>case1:</strong> 注解方式</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(<span class="hljs-string">"classpath*:*.xml"</span>)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BeanTest</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>case2:</strong> 主动加载容器方式</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">private</span> ServiceA serviceA;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">@Before</span></span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">    ApplicationContext apc = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">"classpath:*.xml"</span>);</span><br><span class="line">    serviceA = (ServiceA) apc.getBean(<span class="hljs-string">"serviceA"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-JavaConfig单测使用姿势"><a href="#2-JavaConfig单测使用姿势" class="headerlink" title="2. JavaConfig单测使用姿势"></a>2. JavaConfig单测使用姿势</h3><p>那么替换成JavaConfig的用法，也有两种</p>
<p><strong>case1:</strong> 注解方式，指定内部classes值</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(classes = SpringConfig.class)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SprintUnit</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>case2:</strong> 主动加载容器，改为<code>AnnotationConfigApplicationContext</code></p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Test</span></span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testServiceA</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">    ApplicationContext context = <span class="hljs-keyword">new</span> AnnotationConfigApplicationContext(BeansConfiguration.class);</span><br><span class="line">    ServiceA serviceA = (ServiceA) context.getBean(<span class="hljs-string">"serviceA"</span>);</span><br><span class="line">    serviceA.print();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="III-小结"><a href="#III-小结" class="headerlink" title="III. 小结"></a>III. 小结</h2><h3 id="1-注解映射关系"><a href="#1-注解映射关系" class="headerlink" title="1. 注解映射关系"></a>1. 注解映射关系</h3><p>JavaConfig方式基本上采用的是替换的思路来取代xml，即原xml中的一些东西，可以直接通过注解来代替，如</p>
<ul>
<li>@Configuration 修饰类，与传统的xml文件作用相同</li>
<li>@Bean注解，修饰方法，表示声明一个Bean，与原来的xml中的 <code>&lt;bean&gt;</code> 标签作用相同</li>
<li>@ComponentScan注解，自动扫描包，类似xml中的 <code>&lt;context:component-scan&gt;</code></li>
<li>@Import注解，与xml中的<code>&lt;import&gt;</code>标签类似，引入其他的配置信息</li>
</ul>
<h3 id="2-BeanName重名规则"><a href="#2-BeanName重名规则" class="headerlink" title="2. BeanName重名规则"></a>2. BeanName重名规则</h3><p>在实际使用中，有一点需要额外注意，对于beanName相同的情况，通过测试的规则如下（没有看源码，不保证完全准确，仅为测试后得出的依据）：</p>
<ul>
<li>JavaConfig 定义的BeanName与自动扫描的BeanName冲突时，JavaConfig的定义的会被实例化</li>
<li>JavaConfig 中定义了BeanName相同的Bean时，优先定义的有效（这里不抛异常不太能理解）</li>
<li>自动扫描的Bean，不支持类名相同，但是包路径不同的场景（会抛异常）</li>
</ul>
<h3 id="3-测试姿势"><a href="#3-测试姿势" class="headerlink" title="3. 测试姿势"></a>3. 测试姿势</h3><p>最简单的就是修改原来的注解<code>@ContextConfiguration</code>中的值</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(classes = SpringConfig.class)</span><br></pre></td></tr></table></figure>
<h2 id="II-其他"><a href="#II-其他" class="headerlink" title="II. 其他"></a>II. 其他</h2><h3 id="一灰灰Blog：-https-liuyueyi-github-io-hexblog"><a href="#一灰灰Blog：-https-liuyueyi-github-io-hexblog" class="headerlink" title="一灰灰Blog： https://liuyueyi.github.io/hexblog"></a><a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a>： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">https://liuyueyi.github.io/hexblog</a></h3><p>一灰灰的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因个人能力有限，难免有疏漏和错误之处，如发现bug或者有更好的建议，欢迎批评指正，不吝感激</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="扫描关注"><a href="#扫描关注" class="headerlink" title="扫描关注"></a>扫描关注</h3><p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/05/13/Spring学习之事务管理与传播属性/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                Spring学习之事务管理与传播属性
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-05-13T08:29:17.000Z">2018-05-13</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    17 分钟 读完 (大约 2618 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/05/13/Spring学习之事务管理与传播属性/">Spring学习之事务管理与传播属性</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring-事务管理与传播属性"><a href="#Spring-事务管理与传播属性" class="headerlink" title="Spring 事务管理与传播属性"></a>Spring 事务管理与传播属性</h1><p>在博文 <a href="https://liuyueyi.github.io/hexblog/2018/05/12/Spring%E5%AD%A6%E4%B9%A0%E4%B9%8B%E4%BA%8B%E7%89%A9%E7%9A%84%E4%BD%BF%E7%94%A8%E5%A7%BF%E5%8A%BF/" target="_blank" rel="noopener">《Spring学习之事务的使用姿势》</a> 中，演示了基于注解和xml的事务使用姿势，以<code>@Transactional</code>注解为例，其中很多的参数都没有详细说明</p>
<p>本篇博文，则主要目的是弄懂这些参数有啥用，以及在实际项目中如何选择</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/05/13/Spring学习之事务管理与传播属性/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/05/12/Spring学习之事务的使用姿势/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                Spring学习之事务的使用姿势
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-05-12T14:44:02.000Z">2018-05-12</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    27 分钟 读完 (大约 4026 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/05/12/Spring学习之事务的使用姿势/">Spring学习之事务的使用姿势</a>
            
        </h1>
        <div class="content">
            

            <h1 id="Spring-mybatis-mysql-使用事务的几种姿势"><a href="#Spring-mybatis-mysql-使用事务的几种姿势" class="headerlink" title="Spring + mybatis + mysql 使用事务的几种姿势"></a>Spring + mybatis + mysql 使用事务的几种姿势</h1><p>主要记录下spring是如何支持事务的，以及在Spring结合mybatis时，可以怎么简单的实现数据库的事务功能</p>
<h2 id="I-前提"><a href="#I-前提" class="headerlink" title="I. 前提"></a>I. 前提</h2><h3 id="case1：两张表的的事务支持情况"><a href="#case1：两张表的的事务支持情况" class="headerlink" title="case1：两张表的的事务支持情况"></a>case1：两张表的的事务支持情况</h3><p>首先准备两张表，一个user表，一个story表，结构如下</p>
<figure class="highlight sql hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`user`</span> (</span><br><span class="line">  <span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">''</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'用户名'</span>,</span><br><span class="line">  <span class="hljs-string">`pwd`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">26</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">''</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'密码'</span>,</span><br><span class="line">  <span class="hljs-string">`isDeleted`</span> tinyint(<span class="hljs-number">1</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`created`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`updated`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>),</span><br><span class="line">  <span class="hljs-keyword">KEY</span> <span class="hljs-string">`name`</span> (<span class="hljs-string">`name`</span>)</span><br><span class="line">) <span class="hljs-keyword">ENGINE</span>=<span class="hljs-keyword">InnoDB</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">CHARSET</span>=utf8mb4;</span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`story`</span> (</span><br><span class="line">  <span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="hljs-string">`userId`</span> <span class="hljs-built_in">int</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'作者的userID'</span>,</span><br><span class="line">  <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">''</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'作者名'</span>,</span><br><span class="line">  <span class="hljs-string">`title`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">26</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">''</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'密码'</span>,</span><br><span class="line">  <span class="hljs-string">`story`</span> <span class="hljs-built_in">text</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'故事内容'</span>,</span><br><span class="line">  <span class="hljs-string">`isDeleted`</span> tinyint(<span class="hljs-number">1</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`created`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`updated`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>),</span><br><span class="line">  <span class="hljs-keyword">KEY</span> <span class="hljs-string">`userId`</span> (<span class="hljs-string">`userId`</span>)</span><br><span class="line">) <span class="hljs-keyword">ENGINE</span>=<span class="hljs-keyword">InnoDB</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">CHARSET</span>=utf8mb4;</span><br></pre></td></tr></table></figure>
<p>我们的事务场景在于用户修改name时，要求两张表的name都需要一起修改，不允许出现不一致的情况</p>
<h3 id="case2：单表的事务支持"><a href="#case2：单表的事务支持" class="headerlink" title="case2：单表的事务支持"></a>case2：单表的事务支持</h3><p>转账，一个用户减钱，另一个用户加钱</p>
<figure class="highlight sql hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`money`</span> (</span><br><span class="line">  <span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">''</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'用户名'</span>,</span><br><span class="line">  <span class="hljs-string">`money`</span> <span class="hljs-built_in">int</span>(<span class="hljs-number">26</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'钱'</span>,</span><br><span class="line">  <span class="hljs-string">`isDeleted`</span> tinyint(<span class="hljs-number">1</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`created`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  <span class="hljs-string">`updated`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">13</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span>,</span><br><span class="line">  PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>),</span><br><span class="line">  <span class="hljs-keyword">KEY</span> <span class="hljs-string">`name`</span> (<span class="hljs-string">`name`</span>)</span><br><span class="line">) <span class="hljs-keyword">ENGINE</span>=<span class="hljs-keyword">InnoDB</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">CHARSET</span>=utf8mb4;</span><br></pre></td></tr></table></figure>
<p>相比上面那个case，这个更加简单了，下面的实例则主要根据这个进行说明，至于case1，则留待扩展里面进行</p>
<p>首先是实现对应的dao和entity</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Data</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MoneyEntity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = -<span class="hljs-number">7074788842783160025L</span>;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> id;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> money;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> isDeleted;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> created;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> updated;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MoneyDao</span> </span>&#123;</span><br><span class="line">    <span class="hljs-function">MoneyEntity <span class="hljs-title">queryMoney</span><span class="hljs-params">(@Param(<span class="hljs-string">"id"</span>)</span> <span class="hljs-keyword">int</span> userId)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">// 加钱，负数时表示减钱</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">incrementMoney</span><span class="hljs-params">(@Param(<span class="hljs-string">"id"</span>)</span> <span class="hljs-keyword">int</span> userId, @<span class="hljs-title">Param</span><span class="hljs-params">(<span class="hljs-string">"addMoney"</span>)</span> <span class="hljs-keyword">int</span> addMoney)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>对应的mapper文件为</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="hljs-meta">&lt;!DOCTYPE mapper</span></span><br><span class="line"><span class="hljs-meta">        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"</span></span><br><span class="line"><span class="hljs-meta">        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">namespace</span>=<span class="hljs-string">"com.git.hui.demo.mybatis.mapper.MoneyDao"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">sql</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"moneyEntity"</span>&gt;</span></span><br><span class="line">        id, `name`, `money`, `isDeleted`, `created`, `updated`</span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"queryMoney"</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">"com.git.hui.demo.mybatis.entity.MoneyEntity"</span>&gt;</span></span><br><span class="line">        select</span><br><span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">include</span> <span class="hljs-attr">refid</span>=<span class="hljs-string">"moneyEntity"</span>/&gt;</span></span><br><span class="line">        from money</span><br><span class="line">        where id=#&#123;id&#125;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"incrementMoney"</span>&gt;</span></span><br><span class="line">        update money</span><br><span class="line">        set money=money + #&#123;addMoney&#125;</span><br><span class="line">        where id=#&#123;id&#125;</span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">update</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>对应的mybatis连接数据源的相关配置</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"locations"</span>&gt;</span></span><br><span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>classpath*:jdbc.properties<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.alibaba.druid.pool.DruidDataSource"</span> <span class="hljs-attr">init-method</span>=<span class="hljs-string">"init"</span> <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">"close"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"driverClassName"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"url"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"filters"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"stat"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxActive"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"20"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"initialSize"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxWait"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"60000"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"minIdle"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"timeBetweenEvictionRunsMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"60000"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"minEvictableIdleTimeMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"300000"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"validationQuery"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"SELECT 'x'"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"testWhileIdle"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"testOnBorrow"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"testOnReturn"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"poolPreparedStatements"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxPoolPreparedStatementPerConnectionSize"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"50"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"sqlSessionFactory"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.mybatis.spring.SqlSessionFactoryBean"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"dataSource"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 指定mapper文件 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"mapperLocations"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"classpath*:mapper/*.xml"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!-- 指定扫描dao --&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"basePackage"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"com.git.hui.demo.mybatis"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="II-实例演示"><a href="#II-实例演示" class="headerlink" title="II. 实例演示"></a>II. 实例演示</h2><p>通过网上查询，Spring事务管理总共有四种方式，下面逐一进行演示，每种方式是怎么玩的，然后看实际项目中应该如何抉择</p>
<h3 id="1-硬编码方式"><a href="#1-硬编码方式" class="headerlink" title="1. 硬编码方式"></a>1. 硬编码方式</h3><p>编程式事务管理，既通过TransactionTemplate来实现多个db操作的事务管理</p>
<h4 id="a-实现"><a href="#a-实现" class="headerlink" title="a. 实现"></a>a. 实现</h4><p>那么，我们的转账case可以如下实现</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Repository</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CodeDemo1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> TransactionTemplate transactionTemplate;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">/**</span></span><br><span class="line"><span class="hljs-comment">     * 转账</span></span><br><span class="line"><span class="hljs-comment">     *</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> inUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> outUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> payMoney</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> status    0 表示正常转账， 1 表示内部抛出一个异常， 2 表示新开一个线程，修改inUserId的钱 +200， 3 表示新开一个线程，修改outUserId的钱 + 200</span></span><br><span class="line"><span class="hljs-comment">     */</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">transfor</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> payMoney, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line">        transactionTemplate.execute(<span class="hljs-keyword">new</span> TransactionCallbackWithoutResult() &#123;</span><br><span class="line">            <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doInTransactionWithoutResult</span><span class="hljs-params">(TransactionStatus transactionStatus)</span> </span>&#123;</span><br><span class="line">                MoneyEntity entity = moneyDao.queryMoney(outUserId);</span><br><span class="line">                <span class="hljs-keyword">if</span> (entity.getMoney() &gt; payMoney) &#123; <span class="hljs-comment">// 可以转账</span></span><br><span class="line"></span><br><span class="line">                    <span class="hljs-comment">// 先减钱</span></span><br><span class="line">                    moneyDao.incrementMoney(outUserId, -payMoney);</span><br><span class="line"></span><br><span class="line">                    </span><br><span class="line">                    testCase(inUserId, outUserId, status);</span><br><span class="line"></span><br><span class="line">                    <span class="hljs-comment">// 再加钱</span></span><br><span class="line">                    moneyDao.incrementMoney(inUserId, payMoney);</span><br><span class="line">                    System.out.println(<span class="hljs-string">"转账完成! now: "</span> + System.currentTimeMillis());</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="hljs-comment">// 下面都是测试用例相关</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testCase</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">if</span> (status == <span class="hljs-number">1</span>) &#123;</span><br><span class="line">            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"转账异常!!!"</span>);</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(status == <span class="hljs-number">2</span>) &#123;</span><br><span class="line">            addMoney(inUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status == <span class="hljs-number">3</span>) &#123;</span><br><span class="line">            addMoney(outUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addMoney</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> userId)</span> </span>&#123;</span><br><span class="line">        System.out.printf(<span class="hljs-string">"内部加钱: "</span> + System.currentTimeMillis());</span><br><span class="line">        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">                moneyDao.incrementMoney(userId, <span class="hljs-number">200</span>);</span><br><span class="line">                System.out.println(<span class="hljs-string">"  sub modify success! now: "</span> + System.currentTimeMillis());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>主要看上面的transfor方法，内部通过 transactionTemplate 来实现事务的封装，内部有三个db操作，一个查询，两个更新，具体分析后面说明</p>
<p>上面的代码比较简单了，唯一需要关注的就是transactionTemplate这个bean如何定义的，xml文件中与前面重复的就不贴了，直接贴上关键代码, 一个是根据DataSource创建的<code>TransactionManager</code>，一个则是根据<code>TransactionManager</code>创建的<code>TransactionTemplate</code></p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">&lt;!--编程式事务--&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"transactionManager"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"dataSource"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"transactionTemplate"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.transaction.support.TransactionTemplate"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"transactionManager"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"transactionManager"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="b-测试用例"><a href="#b-测试用例" class="headerlink" title="b. 测试用例"></a>b. 测试用例</h4><p>正常演示情况, 演示没有任何异常，不考虑并发的情况</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(&#123;<span class="hljs-string">"classpath*:spring/service.xml"</span>, <span class="hljs-string">"classpath*:test-datasource1.xml"</span>&#125;)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CodeDemo1Test</span> </span>&#123;</span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> CodeDemo1 codeDemo1;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Test</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTransfor</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------before----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        codeDemo1.transfor(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------after----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出如下，两个账号的钱都没有问题</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10000</span><br><span class="line">id: 2 money = 50000</span><br><span class="line">转账完成! now: 1526130394266</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>转账过程中出现异常，特别是转账方钱已扣，收款方还没收到钱时，也就是case中的status为1的场景</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">// 内部抛异常的情况</span></span><br><span class="line"><span class="hljs-meta">@Test</span></span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTransforException</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="hljs-string">"---------before----------"</span>);</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">        codeDemo1.transfor(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, <span class="hljs-number">1</span>);</span><br><span class="line">    &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="hljs-string">"---------after----------"</span>);</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>对此，我们希望把转账方的钱还回去, 输出如下，发现两个的钱都没有变化</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">java.lang.IllegalArgumentException: 转账异常!!!</span><br><span class="line">	... // 省略异常信息</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>当status为2，表示在转账人钱已扣，收款人钱没收到之间，又有人给收款人转了200，此时根据mysql的锁机制，另外人的转账应该是立马到的（因为收款人账号没有被锁住），且金额不应该有问题</p>
<p>输出结果如下：</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br><span class="line"><span class="hljs-comment">## 右边是注释: 转账过程中，另外存钱立马到账，没有被锁住</span></span><br><span class="line">内部加钱: 1526130827480</span><br><span class="line">sub modify success! now: 1526130827500 </span><br><span class="line"><span class="hljs-comment">## 存钱结束</span></span><br><span class="line">转账完成! now: 1526130830488</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49980</span><br></pre></td></tr></table></figure>
<hr>
<p>当status为3， 表示在转账人钱已扣，收款人钱没收到之间，又有人给转账人转了200，这时因为转账人的记录以及被加了写锁，因此只能等待转账的事务提交之后，才有可能+200成功，当然最终的金额也得一致</p>
<p>输出结果如下</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49980</span><br><span class="line"><span class="hljs-comment">## 右边是注释：内部存钱了，但没有马上成功</span></span><br><span class="line"><span class="hljs-comment">## 直到转账完成后，才立马存成功，注意两个时间戳</span></span><br><span class="line">内部加钱: 1526131101046</span><br><span class="line">转账完成! now: 1526131104051</span><br><span class="line">sub modify success! now: 1526131104053</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10230</span><br><span class="line">id: 2 money = 50170</span><br></pre></td></tr></table></figure>
<h4 id="c-小结"><a href="#c-小结" class="headerlink" title="c. 小结"></a>c. 小结</h4><p>至此，编程式事务已经实例演示ok，从上面的过程，给人的感觉就和直接写事务相关的sql一样，</p>
<figure class="highlight sql hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">start</span> <span class="hljs-keyword">transaction</span>;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">-- 这中间就是 TransactionTemplate#execute 方法内部的逻辑</span></span><br><span class="line"><span class="hljs-comment">-- 也就是需要事务管理的一组sql</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">commit</span>;</span><br></pre></td></tr></table></figure>
<h3 id="2-基于TransactionProxyFactoryBean方式"><a href="#2-基于TransactionProxyFactoryBean方式" class="headerlink" title="2. 基于TransactionProxyFactoryBean方式"></a>2. 基于<code>TransactionProxyFactoryBean</code>方式</h3><p>接下来的三个就是声明式事务管理，这种用得也比较少，因为需要每个事务管理类，添加一个TransactionProxyFactoryBean</p>
<h4 id="a-实现-1"><a href="#a-实现-1" class="headerlink" title="a. 实现"></a>a. 实现</h4><p>除了将 <code>TransactionTemplate</code> 干掉，并将内部的sql逻辑移除之外，对比前面的，发现基本上没有太多差别</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FactoryBeanDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">/**</span></span><br><span class="line"><span class="hljs-comment">     * 转账</span></span><br><span class="line"><span class="hljs-comment">     *</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> inUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> outUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> payMoney</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> status    0 表示正常转账， 1 表示内部抛出一个异常， 2 表示新开一个线程，修改inUserId的钱 +200， 3 表示新开一个线程，修改outUserId的钱 + 200</span></span><br><span class="line"><span class="hljs-comment">     */</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">transfor</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> payMoney, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        MoneyEntity entity = moneyDao.queryMoney(outUserId);</span><br><span class="line">        <span class="hljs-keyword">if</span> (entity.getMoney() &gt; payMoney) &#123; <span class="hljs-comment">// 可以转账</span></span><br><span class="line"></span><br><span class="line">            <span class="hljs-comment">// 先减钱</span></span><br><span class="line">            moneyDao.incrementMoney(outUserId, -payMoney);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            testCase(inUserId, outUserId, status);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            <span class="hljs-comment">// 再加钱</span></span><br><span class="line">            moneyDao.incrementMoney(inUserId, payMoney);</span><br><span class="line">            System.out.println(<span class="hljs-string">"转账完成! now: "</span> + System.currentTimeMillis());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testCase</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">if</span> (status == <span class="hljs-number">1</span>) &#123;</span><br><span class="line">            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"转账异常!!!"</span>);</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status == <span class="hljs-number">2</span>) &#123;</span><br><span class="line">            addMoney(inUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status == <span class="hljs-number">3</span>) &#123;</span><br><span class="line">            addMoney(outUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addMoney</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> userId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="hljs-string">"内部加钱: "</span> + System.currentTimeMillis());</span><br><span class="line">        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">                moneyDao.incrementMoney(userId, <span class="hljs-number">200</span>);</span><br><span class="line">                System.out.println(<span class="hljs-string">"sub modify success! now: "</span> + System.currentTimeMillis());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>重点来了，主要是需要配置一个 <code>TransactionProxyBeanFactory</code>，我们知道BeanFactory就是我们自己来创建Bean的一种手段，相关的xml配置如下</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">&lt;!--编程式事务--&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"transactionManager"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"dataSource"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"factoryBeanDemo2"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.git.hui.demo.mybatis.repository.transaction.FactoryBeanDemo2"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!-- 配置业务层的代理 --&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"factoryBeanDemoProxy"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.transaction.interceptor.TransactionProxyFactoryBean"</span>&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 配置目标对象 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"target"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"factoryBeanDemo2"</span> /&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 注入事务管理器 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"transactionManager"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"transactionManager"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 注入事务的属性 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"transactionAttributes"</span>&gt;</span></span><br><span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span></span><br><span class="line">            <span class="hljs-comment">&lt;!--</span></span><br><span class="line"><span class="hljs-comment">                prop的格式:</span></span><br><span class="line"><span class="hljs-comment">                    * PROPAGATION   :事务的传播行为</span></span><br><span class="line"><span class="hljs-comment">                    * ISOTATION     :事务的隔离级别</span></span><br><span class="line"><span class="hljs-comment">                    * readOnly      :只读</span></span><br><span class="line"><span class="hljs-comment">                    * -EXCEPTION    :发生哪些异常回滚事务</span></span><br><span class="line"><span class="hljs-comment">                    * +EXCEPTION    :发生哪些异常不回滚事务</span></span><br><span class="line"><span class="hljs-comment">             --&gt;</span></span><br><span class="line">             <span class="hljs-comment">&lt;!-- 这个key对应的就是目标类中的方法--&gt;</span></span><br><span class="line">            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">"transfor"</span>&gt;</span>PROPAGATION_REQUIRED<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span></span><br><span class="line">            <span class="hljs-comment">&lt;!-- &lt;prop key="transfer"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt; --&gt;</span></span><br><span class="line">            <span class="hljs-comment">&lt;!-- &lt;prop key="transfer"&gt;PROPAGATION_REQUIRED,+java.lang.ArithmeticException&lt;/prop&gt; --&gt;</span></span><br><span class="line">        <span class="hljs-tag">&lt;/<span class="hljs-name">props</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>通过上面的配置，大致可以了解到这个通过TransactionProxyFactoryBean就是创建了一个FactoryBeanDemo2的代理类，这个代理类内部封装好事务相关的逻辑，可以看做是前面编程式的一种简单通用抽象</p>
<h4 id="b-测试"><a href="#b-测试" class="headerlink" title="b. 测试"></a>b. 测试</h4><p>测试代码与前面基本相同，唯一的区别就是我们使用的应该是上面BeanFactory生成的Bean，而不是直接使用<code>FactoryBeanDemo2</code></p>
<p>正常演示case:</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(&#123;<span class="hljs-string">"classpath*:spring/service.xml"</span>, <span class="hljs-string">"classpath*:test-datasource2.xml"</span>&#125;)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FactoryBeanDemo1Test</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Resource</span>(name = <span class="hljs-string">"factoryBeanDemoProxy"</span>)</span><br><span class="line">    <span class="hljs-keyword">private</span> FactoryBeanDemo2 factoryBeanDemo2;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Test</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTransfor</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------before----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        factoryBeanDemo2.transfor(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------after----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10000</span><br><span class="line">id: 2 money = 50000</span><br><span class="line">转账完成! now: 1526132058886</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>status为1，内部异常的情况下，我们希望钱也不会有问题</p>
<figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Test</span></span><br><span class="line"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTransforException</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="hljs-string">"---------before----------"</span>);</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">        factoryBeanDemo2.transfor(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, <span class="hljs-number">1</span>);</span><br><span class="line">    &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        System.out.println(e.getMessage());;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="hljs-string">"---------after----------"</span>);</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">    System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出为</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br><span class="line">转账异常!!!</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>status为2 时，分析结果与上面应该相同，输出如下</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49950</span><br><span class="line">内部加钱: 1526133325376</span><br><span class="line">sub modify success! now: 1526133325387</span><br><span class="line">转账完成! now: 1526133328381</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49940</span><br></pre></td></tr></table></figure>
<hr>
<p>status为3时，输出</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49940</span><br><span class="line">内部加钱: 1526133373466</span><br><span class="line">转账完成! now: 1526133376476</span><br><span class="line">sub modify success! now: 1526133376480</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10230</span><br><span class="line">id: 2 money = 50130</span><br></pre></td></tr></table></figure>
<h4 id="c-小结-1"><a href="#c-小结-1" class="headerlink" title="c. 小结"></a>c. 小结</h4><p>TransactionProxyFactoryBean 的思路就是利用代理模式来实现事务管理，生成一个代理类，拦截目标方法，将一组sql的操作封装到事务中进行；相比较于硬编码，无侵入，而且支持灵活的配置方式</p>
<p>缺点也显而易见，每个都要进行配置，比较繁琐</p>
<h3 id="3-xml使用方式"><a href="#3-xml使用方式" class="headerlink" title="3. xml使用方式"></a>3. xml使用方式</h3><p>Spring有两大特点，IoC和AOP，对于事务这种情况而言，我们可不可以使用AOP来做呢？</p>
<p>对于需要开启事务的方法，拦截掉，执行前开始事务，执行完毕之后提交事务，出现异常时回滚</p>
<p>这样一看，感觉还是蛮有希望的，而下面两种姿势正是这么玩的，因此需要加上aspect的依赖</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.aspectj<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>aspectjweaver<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.8.7<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="a-实现-2"><a href="#a-实现-2" class="headerlink" title="a. 实现"></a>a. 实现</h4><p>java类与第二种完全一致，变动的只有xml</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">&lt;!-- 首先添加命名空间 --&gt;</span></span><br><span class="line">xmlns:tx="http://www.springframework.org/schema/tx" </span><br><span class="line">xmlns:aop="http://www.springframework.org/schema/aop"</span><br><span class="line">xsi:schemaLocation="...</span><br><span class="line">  http://www.springframework.org/schema/tx</span><br><span class="line"> http://www.springframework.org/schema/tx/spring-tx.xsd"</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!--对应的事务通知和切面配置--&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tx:advice</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"txAdvice"</span> <span class="hljs-attr">transaction-manager</span>=<span class="hljs-string">"transactionManager"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">tx:attributes</span>&gt;</span></span><br><span class="line">        <span class="hljs-comment">&lt;!--</span></span><br><span class="line"><span class="hljs-comment">            propagation :事务传播行为</span></span><br><span class="line"><span class="hljs-comment">            isolation   :事务的隔离级别</span></span><br><span class="line"><span class="hljs-comment">            read-only   :只读</span></span><br><span class="line"><span class="hljs-comment">            rollback-for:发生哪些异常回滚</span></span><br><span class="line"><span class="hljs-comment">            no-rollback-for :发生哪些异常不回滚</span></span><br><span class="line"><span class="hljs-comment">            timeout     :过期信息</span></span><br><span class="line"><span class="hljs-comment">         --&gt;</span></span><br><span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">tx:method</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"transfor"</span> <span class="hljs-attr">propagation</span>=<span class="hljs-string">"REQUIRED"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">tx:attributes</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tx:advice</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!-- 配置切面 --&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 配置切入点 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">aop:pointcut</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">"execution(* com.git.hui.demo.mybatis.repository.transaction.XmlDemo3.*(..))"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"pointcut1"</span>/&gt;</span></span><br><span class="line">    <span class="hljs-comment">&lt;!-- 配置切面 --&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">aop:advisor</span> <span class="hljs-attr">advice-ref</span>=<span class="hljs-string">"txAdvice"</span> <span class="hljs-attr">pointcut-ref</span>=<span class="hljs-string">"pointcut1"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>观察上面的配置，再想想第二种方式，思路都差不多了，但是这种方式明显更加通用，通过切面和切点，可以减少大量的配置</p>
<h4 id="b-测试-1"><a href="#b-测试-1" class="headerlink" title="b. 测试"></a>b. 测试</h4><figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="line"><span class="hljs-meta">@ContextConfiguration</span>(&#123;<span class="hljs-string">"classpath*:spring/service.xml"</span>, <span class="hljs-string">"classpath*:test-datasource3.xml"</span>&#125;)</span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">XmlBeanTest</span> </span>&#123;</span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> XmlDemo3 xmlDemo;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Test</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTransfor</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------before----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        xmlDemo.transfor(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="hljs-string">"---------after----------"</span>);</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 1 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">1</span>).getMoney());</span><br><span class="line">        System.out.println(<span class="hljs-string">"id: 2 money = "</span> + moneyDao.queryMoney(<span class="hljs-number">2</span>).getMoney());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个测试起来，和一般的写法就没啥两样了，比第二种的FactoryBean的注入方式简单点</p>
<p>正常输出</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10000</span><br><span class="line">id: 2 money = 50000</span><br><span class="line">转账完成! now: 1526135301273</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>status=1 出现异常时，输出</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br><span class="line">转账异常!!!</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br></pre></td></tr></table></figure>
<hr>
<p>status=2 转账过程中，又存钱的场景，输出，与前面预期一致</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10010</span><br><span class="line">id: 2 money = 49990</span><br><span class="line">内部加钱: 1526135438403</span><br><span class="line">sub modify success! now: 1526135438421</span><br><span class="line">转账完成! now: 1526135441410</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49980</span><br></pre></td></tr></table></figure>
<hr>
<p>status=3 的输出，与前面预期一致</p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">---------before----------</span><br><span class="line">id: 1 money = 10220</span><br><span class="line">id: 2 money = 49980</span><br><span class="line">内部加钱: 1526135464341</span><br><span class="line">转账完成! now: 1526135467349</span><br><span class="line">sub modify success! now: 1526135467352</span><br><span class="line">---------after----------</span><br><span class="line">id: 1 money = 10230</span><br><span class="line">id: 2 money = 50170</span><br></pre></td></tr></table></figure>
<h3 id="4-注解方式"><a href="#4-注解方式" class="headerlink" title="4. 注解方式"></a>4. 注解方式</h3><p>这个就是消灭xml，用注解来做的方式，就是将前面xml中的配置用 @Transactional注解替换</p>
<h4 id="a-实现-3"><a href="#a-实现-3" class="headerlink" title="a. 实现"></a>a. 实现</h4><figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Repository</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AnnoDemo4</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Autowired</span></span><br><span class="line">    <span class="hljs-keyword">private</span> MoneyDao moneyDao;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">/**</span></span><br><span class="line"><span class="hljs-comment">     * 转账</span></span><br><span class="line"><span class="hljs-comment">     *</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> inUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> outUserId</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> payMoney</span></span><br><span class="line"><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> status    0 表示正常转账， 1 表示内部抛出一个异常， 2 表示新开一个线程，修改inUserId的钱 +200， 3 表示新开一个线程，修改outUserId的钱 + 200</span></span><br><span class="line"><span class="hljs-comment">     *</span></span><br><span class="line"><span class="hljs-comment">     *</span></span><br><span class="line"><span class="hljs-comment">     * Transactional注解中的的属性 propagation :事务的传播行为 isolation :事务的隔离级别 readOnly :只读</span></span><br><span class="line"><span class="hljs-comment">     * rollbackFor :发生哪些异常回滚 noRollbackFor :发生哪些异常不回滚</span></span><br><span class="line"><span class="hljs-comment">     * rollbackForClassName 根据异常类名回滚</span></span><br><span class="line"><span class="hljs-comment">     */</span></span><br><span class="line">    <span class="hljs-meta">@Transactional</span>(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = <span class="hljs-keyword">false</span>)</span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">transfor</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> payMoney, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        MoneyEntity entity = moneyDao.queryMoney(outUserId);</span><br><span class="line">        <span class="hljs-keyword">if</span> (entity.getMoney() &gt; payMoney) &#123; <span class="hljs-comment">// 可以转账</span></span><br><span class="line"></span><br><span class="line">            <span class="hljs-comment">// 先减钱</span></span><br><span class="line">            moneyDao.incrementMoney(outUserId, -payMoney);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            testCase(inUserId, outUserId, status);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            <span class="hljs-comment">// 再加钱</span></span><br><span class="line">            moneyDao.incrementMoney(inUserId, payMoney);</span><br><span class="line">            System.out.println(<span class="hljs-string">"转账完成! now: "</span> + System.currentTimeMillis());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testCase</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> inUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> outUserId, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> status)</span> </span>&#123;</span><br><span class="line">        <span class="hljs-keyword">if</span> (status == <span class="hljs-number">1</span>) &#123;</span><br><span class="line">            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"转账异常!!!"</span>);</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status == <span class="hljs-number">2</span>) &#123;</span><br><span class="line">            addMoney(inUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status == <span class="hljs-number">3</span>) &#123;</span><br><span class="line">            addMoney(outUserId);</span><br><span class="line">            <span class="hljs-keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="hljs-number">3000</span>);</span><br><span class="line">            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addMoney</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> userId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="hljs-string">"内部加钱: "</span> + System.currentTimeMillis());</span><br><span class="line">        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>&#123;</span><br><span class="line">                moneyDao.incrementMoney(userId, <span class="hljs-number">200</span>);</span><br><span class="line">                System.out.println(<span class="hljs-string">"sub modify success! now: "</span> + System.currentTimeMillis());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因此需要在xml中配置，开启事务注解</p>
<figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!--编程式事务--&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"transactionManager"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>&gt;</span></span><br><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"dataSource"</span>/&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tx:annotation-driven</span> <span class="hljs-attr">transaction-manager</span>=<span class="hljs-string">"transactionManager"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>这样一看，就更加清晰了，实际项目中，xml和注解方式也是用得最多的场景了</p>
<h4 id="b-测试case"><a href="#b-测试case" class="headerlink" title="b. 测试case"></a>b. 测试case</h4><p>和第三种测试case完全相同, 输出结果也一样，直接省略</p>
<h2 id="III-小结"><a href="#III-小结" class="headerlink" title="III. 小结"></a>III. 小结</h2><p>上面说了Spring中四种使用事务的姿势，其中硬编码方式可能是最好理解的，就相当于将我们写sql中，使用事务的方式直接翻译成对应的java代码了；而FactoryBean方式相当于特殊情况特殊对待，为每个事务来一个代理类来增强事务功能；后面的两个则原理差不多都是利用事务通知（AOP)来实现，定义切点及相关信息</p>
<p><strong>编程式：</strong></p>
<ul>
<li>注入 <code>TransactionTemplate</code> </li>
<li>将利用事务的逻辑封装到 <code>transactionTemplate#execute</code>方法内</li>
</ul>
<p><strong>代理BeanFactory：</strong></p>
<ul>
<li>利用 <code>TransactionProxyFactoryBean</code> 为事务相关类生成代理</li>
<li>使用方通过FactoryBean获取代理类，作为使用的Bean</li>
</ul>
<p><strong>xml配置：</strong></p>
<ul>
<li>利用 tx标签 + aop方式来实现</li>
<li><code>&lt;tx:advice&gt;</code> 标签定义事务通知，内部可有较多的配置信息</li>
<li><code>&lt;aop:config&gt;</code> 配置切点，切面</li>
</ul>
<p><strong>注解方式：</strong></p>
<ul>
<li>在开启事务的方法or类上添加 <code>@Transactional</code> 注解即可</li>
<li>开启事务注解 <code>&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;/&gt;</code></li>
</ul>
<h2 id="IV-其他"><a href="#IV-其他" class="headerlink" title="IV. 其他"></a>IV. 其他</h2><h3 id="1-参考"><a href="#1-参考" class="headerlink" title="1. 参考"></a>1. 参考</h3><p><strong>文档</strong></p>
<ul>
<li><a href="https://blog.csdn.net/daijin888888/article/details/51822257" target="_blank" rel="noopener">Spring事务管理的四种方式</a></li>
</ul>
<p><strong>源码</strong></p>
<ul>
<li>项目源码：<a href="https://gitee.com/liuyueyi/study-demo/tree/master/dao" target="_blank" rel="noopener">study-demo</a></li>
<li>主要查看包路径： <a href="https://gitee.com/liuyueyi/study-demo/tree/master/dao/src/main/java/com/git/hui/demo/mybatis/repository/transaction" target="_blank" rel="noopener">事务demo</a></li>
<li>测试相关代码： <a href="https://gitee.com/liuyueyi/study-demo/tree/master/dao/src/test/java/com/git/hui/demo/test/transaction" target="_blank" rel="noopener">测试demo</a></li>
</ul>
<h3 id="2-个人博客：-一灰灰Blog"><a href="#2-个人博客：-一灰灰Blog" class="headerlink" title="2. 个人博客： 一灰灰Blog"></a>2. 个人博客： <a href="https://liuyueyi.github.io/hexblog" target="_blank" rel="noopener">一灰灰Blog</a></h3><p>基于hexo + github pages搭建的个人博客，记录所有学习和工作中的博文，欢迎大家前去逛逛</p>
<h3 id="3-声明"><a href="#3-声明" class="headerlink" title="3. 声明"></a>3. 声明</h3><p>尽信书则不如，已上内容，纯属一家之言，因本人能力一般，见识有限，如发现bug或者有更好的建议，随时欢迎批评指正</p>
<ul>
<li>微博地址: <a href="https://weibo.com/p/1005052169825577/home" target="_blank" rel="noopener">小灰灰Blog</a></li>
<li>QQ： 一灰灰/3302797840</li>
</ul>
<h3 id="4-扫描关注"><a href="#4-扫描关注" class="headerlink" title="4. 扫描关注"></a>4. 扫描关注</h3><p><img src="https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png" alt="QrCode"></p>


            
        </div>
        
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/02/04/SpringMVC统一异常处理/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                SpringMVC统一异常处理
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-02-04T11:21:15.000Z">2018-02-04</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    2 分钟 读完 (大约 252 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/02/04/SpringMVC统一异常处理/">SpringMVC统一异常处理</a>
            
        </h1>
        <div class="content">
            

            <h1 id="统一异常拦截处理方式"><a href="#统一异常拦截处理方式" class="headerlink" title="统一异常拦截处理方式"></a>统一异常拦截处理方式</h1><blockquote>
<p>项目中不可避免会出现一些异常情况，而一个web项目，若不拦截异常，糟糕的情况下可能直接将堆栈抛给前端，从而导致各种鬼畜的问题</p>
</blockquote>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/02/04/SpringMVC统一异常处理/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/01/19/SpringMVC支持跨域的几种姿势/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                SpringMVC支持跨域的几种姿势
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-01-19T07:43:38.000Z">2018-01-19</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    7 分钟 读完 (大约 1101 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/01/19/SpringMVC支持跨域的几种姿势/">SpringMVC支持跨域的几种姿势</a>
            
        </h1>
        <div class="content">
            

            <h1 id="SpringMVC支持跨域的几种姿势"><a href="#SpringMVC支持跨域的几种姿势" class="headerlink" title="SpringMVC支持跨域的几种姿势"></a>SpringMVC支持跨域的几种姿势</h1><blockquote>
<p>跨域好像是一个前端的问题，通常是a域名下向b域名的服务发起请求，然后处于浏览器的安全原则，被拦截了，而这种场景，在实际的项目中并不少见，那么作为后端可以怎么去支持跨域的case呢？</p>
</blockquote>
<p>后端需要支持跨域，一个是支持jsonp请求；还有一个就是设置responseHeader中crossOrigin等相关参数</p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/01/19/SpringMVC支持跨域的几种姿势/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/01/18/SpringMVC返回图片的几种方式/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                SpringMVC返回图片的几种方式
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-01-18T03:13:22.000Z">2018-01-18</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    10 分钟 读完 (大约 1558 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/01/18/SpringMVC返回图片的几种方式/">SpringMVC返回图片的几种方式</a>
            
        </h1>
        <div class="content">
            

            <h1 id="SpringMVC返回图片的几种方式"><a href="#SpringMVC返回图片的几种方式" class="headerlink" title="SpringMVC返回图片的几种方式"></a>SpringMVC返回图片的几种方式</h1><blockquote>
<p>后端提供服务，通常返回的json串，但是某些场景下可能需要直接返回二进制流，如一个图片编辑接口，希望直接将图片流返回给前端；如果要求返回base64，此时可以怎么处理？</p>
</blockquote>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/01/18/SpringMVC返回图片的几种方式/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








    <div class="card">
    
        <a href="/hexblog/2018/01/04/SpringMVC之请求参数的获取方式/">
            <div class="thumbnail default_logo">
                <br/>
                <span >
                SpringMVC之请求参数的获取方式
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-size:0.7em">by 一灰灰</span>
                </span>
            </div>
            </a>
    

    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2018-01-04T08:48:39.000Z">2018-01-04</time>
                
                <div class="level-item">
                <a class="has-link-black -link" href="/hexblog/categories/开源/">开源</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/">Spring</a>&nbsp;/&nbsp;<a class="has-link-black -link" href="/hexblog/categories/开源/Spring/使用小结/">使用小结</a>
                </div>
                
                
                <span class="level-item has-text-grey" style='font-size: 1.2em;'>
                    
                    
                    17 分钟 读完 (大约 2570 个字)
                </span>
                
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/hexblog/2018/01/04/SpringMVC之请求参数的获取方式/">SpringMVC之请求参数的获取方式</a>
            
        </h1>
        <div class="content">
            

            <h1 id="SpringMVC之请求参数的获取方式"><a href="#SpringMVC之请求参数的获取方式" class="headerlink" title="SpringMVC之请求参数的获取方式"></a>SpringMVC之请求参数的获取方式</h1><blockquote>
<p>常见的一个web服务，如何获取请求参数？</p>
</blockquote>
<p>一般最常见的请求为GET和POST，get请求的参数在url上可以获取，post请求参数除了url上还有可能在表单中，文件上传时，获取方式又和一般的参数获取不一样</p>
<p><strong>本篇则主要集中在不同请求方式下，获取参数的使用姿势</strong></p>
<p>首先需要搭建一个后端的请求，为了快速演示</p>
<p>利用spring-boot创建了一个机器简单的工程，依赖版本 <code>1.5.4.RELEASE</code></p>

            
        </div>
        
        
        <div class="level is-mobile">
            <div class="level-start">
                <div class="level-item">
                <a class="button is-size-7 is-light" href="/hexblog/2018/01/04/SpringMVC之请求参数的获取方式/#more">阅读更多</a>
                </div>
            </div>
        </div>
        
        
    </div>
</div>








</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content card-info">
        <nav class="level">
            <div class="level-item has-text-centered">
                <div>
                    
                        <img class="image is-128x128 has-mb-6" src="/hexblog/images/avatar.jpg" alt="一灰灰Blog">
                    
                    
                    <p class="is-size-4 is-block">
                        一灰灰Blog
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        Java，服务器后端开发
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>Wuhan, China</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level is-mobile">
            <div class="level-item has-text-centered is-marginless">
                <div>
                    
                    <p class="heading">
                        文章
                    </p>
                    <a href="/hexblog/archives">
                    <p class="title has-text-weight-normal">
                        269
                    </p>
                    </a>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    
                    <p class="heading">
                        分类
                    </p>
                    <a href="/hexblog/categories">
                        <p class="title has-text-weight-normal">
                            70
                        </p>
                    </a>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                
                    <p class="heading">
                        标签
                    </p>
                    <a href="/hexblog/tags">
                    <p class="title has-text-weight-normal">
                        74
                    </p>
                    </a>
                </div>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://github.com/liuyueyi" target="_blank">
                关注我</a>
        </div>
        
        
        <div class="level is-mobile">
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="Github" href="https://github.com/liuyueyi">
                
                <i class="fab fa-github"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="Gitee" href="https://gitee.com/liuyueyi">
                
                <i class="fab fa-gg"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="Weibo" href="https://weibo.com/p/1005052169825577/home">
                
                <i class="fab fa-weibo"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="weixin" href="https://s10.mogucdn.com/mlcdn/c45406/171229_1cgld3igbelkbc70cd8af1j3809kb_150x150.jpg">
                
                <i class="fab fa-weixin"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="RSS" href="/hexblog/atom.xml">
                
                <i class="fas fa-rss"></i>
                
            </a>
            
        </div>
        
    </div>
</div>
    
        
    
        

<div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            链接
        </h3>
        <ul class="menu-list">
        
            <li>
                <a class="level is-mobile" href="http://spring.hhui.top" target="_blank">
                    <span class="level-left">
                        <span class="level-item">SpringBlog</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">spring.hhui.top</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="https://github.com/liuyueyi" target="_blank">
                    <span class="level-left">
                        <span class="level-item">GitHub</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">github.com</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="https://zweb.hhui.top" target="_blank">
                    <span class="level-left">
                        <span class="level-item">多媒体工具小站</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">zweb.hhui.top</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="https://mweb.hhui.top" target="_blank">
                    <span class="level-left">
                        <span class="level-item">每日十首古诗词</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">mweb.hhui.top</span>
                    </span>
                </a>
            </li>
        
        </ul>
        </div>
    </div>
</div>


    
        
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            最新文章
        </h3>
        
        <article class="media">
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-28T11:27:12.000Z">2020-03-28</time></div>
                    <a href="/hexblog/2020/03/28/200328-MongoDb系列教程九-文档-Document-查询基础篇/" class="has-link-black-ter is-size-6">200328-MongoDb系列教程九：文档 Document 查询基础篇</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/hexblog/categories/DB/">DB</a> / <a class="has-link-grey -link" href="/hexblog/categories/DB/Mongo/">Mongo</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-27T03:04:36.000Z">2020-03-27</time></div>
                    <a href="/hexblog/2020/03/27/200327-MongoDb系列教程八-文档-Document-更新姿势/" class="has-link-black-ter is-size-6">200327-MongoDb系列教程八：文档 Document 更新姿势</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/hexblog/categories/DB/">DB</a> / <a class="has-link-grey -link" href="/hexblog/categories/DB/Mongo/">Mongo</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-26T10:02:03.000Z">2020-03-26</time></div>
                    <a href="/hexblog/2020/03/26/200326-MongoDb系列教程七-文档-Document-删除姿势/" class="has-link-black-ter is-size-6">200326-MongoDb系列教程七：文档 Document 删除姿势</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/hexblog/categories/DB/">DB</a> / <a class="has-link-grey -link" href="/hexblog/categories/DB/Mongo/">Mongo</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-26T09:04:06.000Z">2020-03-26</time></div>
                    <a href="/hexblog/2020/03/26/200326-MongoDb系列教程六-文档-Document-插入姿势/" class="has-link-black-ter is-size-6">200326-MongoDb系列教程六：文档 Document 插入姿势</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/hexblog/categories/DB/">DB</a> / <a class="has-link-grey -link" href="/hexblog/categories/DB/Mongo/">Mongo</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-26T08:52:20.000Z">2020-03-26</time></div>
                    <a href="/hexblog/2020/03/26/200326-MongoDb系列教程五-集合-Collection/" class="has-link-black-ter is-size-6">200326-MongoDb系列教程五：集合 Collection</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/hexblog/categories/DB/">DB</a> / <a class="has-link-grey -link" href="/hexblog/categories/DB/Mongo/">Mongo</a>
                    </p>
                </div>
            </div>
        </article>
        
    </div>
</div>

    
        
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            标签云
        </h3>
        <a href="/hexblog/tags/Android/" style="font-size: 10px;">Android</a> <a href="/hexblog/tags/AutoCloseable/" style="font-size: 10px;">AutoCloseable</a> <a href="/hexblog/tags/BloomFilter/" style="font-size: 10px;">BloomFilter</a> <a href="/hexblog/tags/BugFix/" style="font-size: 10.63px;">BugFix</a> <a href="/hexblog/tags/Bugfix/" style="font-size: 10.63px;">Bugfix</a> <a href="/hexblog/tags/Docker/" style="font-size: 11.88px;">Docker</a> <a href="/hexblog/tags/FastJson/" style="font-size: 10px;">FastJson</a> <a href="/hexblog/tags/Git/" style="font-size: 11.25px;">Git</a> <a href="/hexblog/tags/Groovy/" style="font-size: 10px;">Groovy</a> <a href="/hexblog/tags/Guava/" style="font-size: 11.25px;">Guava</a> <a href="/hexblog/tags/HashMap/" style="font-size: 10px;">HashMap</a> <a href="/hexblog/tags/IO/" style="font-size: 10.63px;">IO</a> <a href="/hexblog/tags/ImageMagic/" style="font-size: 10.63px;">ImageMagic</a> <a href="/hexblog/tags/InfluxDB/" style="font-size: 16.88px;">InfluxDB</a> <a href="/hexblog/tags/InputStream/" style="font-size: 10px;">InputStream</a> <a href="/hexblog/tags/JDK/" style="font-size: 15px;">JDK</a> <a href="/hexblog/tags/JVM/" style="font-size: 11.25px;">JVM</a> <a href="/hexblog/tags/Java/" style="font-size: 20px;">Java</a> <a href="/hexblog/tags/JavaAgent/" style="font-size: 10.63px;">JavaAgent</a> <a href="/hexblog/tags/JavaWeb/" style="font-size: 10.63px;">JavaWeb</a> <a href="/hexblog/tags/Jquery/" style="font-size: 10px;">Jquery</a> <a href="/hexblog/tags/Linux/" style="font-size: 15px;">Linux</a> <a href="/hexblog/tags/List/" style="font-size: 10px;">List</a> <a href="/hexblog/tags/MD5/" style="font-size: 10px;">MD5</a> <a href="/hexblog/tags/Map/" style="font-size: 10px;">Map</a> <a href="/hexblog/tags/Maven/" style="font-size: 10px;">Maven</a> <a href="/hexblog/tags/Mongo/" style="font-size: 10.63px;">Mongo</a> <a href="/hexblog/tags/MongoDB/" style="font-size: 10px;">MongoDB</a> <a href="/hexblog/tags/MongoDb/" style="font-size: 14.38px;">MongoDb</a> <a href="/hexblog/tags/MySql/" style="font-size: 10.63px;">MySql</a> <a href="/hexblog/tags/Mybatis/" style="font-size: 10.63px;">Mybatis</a> <a href="/hexblog/tags/Mysql/" style="font-size: 16.25px;">Mysql</a> <a href="/hexblog/tags/Nginx/" style="font-size: 11.25px;">Nginx</a> <a href="/hexblog/tags/OGNL/" style="font-size: 11.25px;">OGNL</a> <a href="/hexblog/tags/ProtoStuff/" style="font-size: 10.63px;">ProtoStuff</a> <a href="/hexblog/tags/Python/" style="font-size: 19.38px;">Python</a> <a href="/hexblog/tags/QuickAlarm/" style="font-size: 10px;">QuickAlarm</a> <a href="/hexblog/tags/RabbitMQ/" style="font-size: 13.75px;">RabbitMQ</a> <a href="/hexblog/tags/ReactJS/" style="font-size: 10px;">ReactJS</a> <a href="/hexblog/tags/Redis/" style="font-size: 14.38px;">Redis</a> <a href="/hexblog/tags/Shell/" style="font-size: 15.63px;">Shell</a> <a href="/hexblog/tags/Socket/" style="font-size: 10px;">Socket</a> <a href="/hexblog/tags/Solr/" style="font-size: 10px;">Solr</a> <a href="/hexblog/tags/Spring/" style="font-size: 18.75px;">Spring</a> <a href="/hexblog/tags/SpringBoot/" style="font-size: 10px;">SpringBoot</a> <a href="/hexblog/tags/Vue/" style="font-size: 10px;">Vue</a> <a href="/hexblog/tags/WebSocket/" style="font-size: 10px;">WebSocket</a> <a href="/hexblog/tags/Yaml/" style="font-size: 10px;">Yaml</a> <a href="/hexblog/tags/css/" style="font-size: 11.25px;">css</a> <a href="/hexblog/tags/ffmpeg/" style="font-size: 10px;">ffmpeg</a> <a href="/hexblog/tags/gitalk/" style="font-size: 10px;">gitalk</a> <a href="/hexblog/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/hexblog/tags/jdk/" style="font-size: 10px;">jdk</a> <a href="/hexblog/tags/logger/" style="font-size: 10px;">logger</a> <a href="/hexblog/tags/markdown/" style="font-size: 10px;">markdown</a> <a href="/hexblog/tags/python/" style="font-size: 10px;">python</a> <a href="/hexblog/tags/time/" style="font-size: 10px;">time</a> <a href="/hexblog/tags/乱码/" style="font-size: 10px;">乱码</a> <a href="/hexblog/tags/二维码/" style="font-size: 10px;">二维码</a> <a href="/hexblog/tags/分库分表/" style="font-size: 10px;">分库分表</a> <a href="/hexblog/tags/反射/" style="font-size: 11.25px;">反射</a> <a href="/hexblog/tags/工具/" style="font-size: 12.5px;">工具</a> <a href="/hexblog/tags/并发/" style="font-size: 11.25px;">并发</a> <a href="/hexblog/tags/序列化/" style="font-size: 10px;">序列化</a> <a href="/hexblog/tags/手记/" style="font-size: 13.13px;">手记</a> <a href="/hexblog/tags/技术方案/" style="font-size: 18.13px;">技术方案</a> <a href="/hexblog/tags/指南/" style="font-size: 13.13px;">指南</a> <a href="/hexblog/tags/教程/" style="font-size: 17.5px;">教程</a> <a href="/hexblog/tags/方案设计/" style="font-size: 10px;">方案设计</a> <a href="/hexblog/tags/时区/" style="font-size: 10px;">时区</a> <a href="/hexblog/tags/时间窗口/" style="font-size: 11.25px;">时间窗口</a> <a href="/hexblog/tags/爬虫/" style="font-size: 12.5px;">爬虫</a> <a href="/hexblog/tags/问题记录/" style="font-size: 10px;">问题记录</a> <a href="/hexblog/tags/随笔/" style="font-size: 10px;">随笔</a>
    </div>
</div>

    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                标签
            </h3>
            <div class="field is-grouped is-grouped-multiline">
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Android/">
                        <span class="tag">Android</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/AutoCloseable/">
                        <span class="tag">AutoCloseable</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/BloomFilter/">
                        <span class="tag">BloomFilter</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/BugFix/">
                        <span class="tag">BugFix</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Bugfix/">
                        <span class="tag">Bugfix</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Docker/">
                        <span class="tag">Docker</span>
                        <span class="tag is-grey">4</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/FastJson/">
                        <span class="tag">FastJson</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Git/">
                        <span class="tag">Git</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Groovy/">
                        <span class="tag">Groovy</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Guava/">
                        <span class="tag">Guava</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/HashMap/">
                        <span class="tag">HashMap</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/IO/">
                        <span class="tag">IO</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/ImageMagic/">
                        <span class="tag">ImageMagic</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/InfluxDB/">
                        <span class="tag">InfluxDB</span>
                        <span class="tag is-grey">16</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/InputStream/">
                        <span class="tag">InputStream</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/JDK/">
                        <span class="tag">JDK</span>
                        <span class="tag is-grey">11</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/JVM/">
                        <span class="tag">JVM</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Java/">
                        <span class="tag">Java</span>
                        <span class="tag is-grey">59</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/JavaAgent/">
                        <span class="tag">JavaAgent</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/JavaWeb/">
                        <span class="tag">JavaWeb</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Jquery/">
                        <span class="tag">Jquery</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Linux/">
                        <span class="tag">Linux</span>
                        <span class="tag is-grey">11</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/List/">
                        <span class="tag">List</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/MD5/">
                        <span class="tag">MD5</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Map/">
                        <span class="tag">Map</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Maven/">
                        <span class="tag">Maven</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Mongo/">
                        <span class="tag">Mongo</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/MongoDB/">
                        <span class="tag">MongoDB</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/MongoDb/">
                        <span class="tag">MongoDb</span>
                        <span class="tag is-grey">9</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/MySql/">
                        <span class="tag">MySql</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Mybatis/">
                        <span class="tag">Mybatis</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Mysql/">
                        <span class="tag">Mysql</span>
                        <span class="tag is-grey">15</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Nginx/">
                        <span class="tag">Nginx</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/OGNL/">
                        <span class="tag">OGNL</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/ProtoStuff/">
                        <span class="tag">ProtoStuff</span>
                        <span class="tag is-grey">2</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Python/">
                        <span class="tag">Python</span>
                        <span class="tag is-grey">32</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/QuickAlarm/">
                        <span class="tag">QuickAlarm</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/RabbitMQ/">
                        <span class="tag">RabbitMQ</span>
                        <span class="tag is-grey">8</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/ReactJS/">
                        <span class="tag">ReactJS</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Redis/">
                        <span class="tag">Redis</span>
                        <span class="tag is-grey">9</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Shell/">
                        <span class="tag">Shell</span>
                        <span class="tag is-grey">13</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Socket/">
                        <span class="tag">Socket</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Solr/">
                        <span class="tag">Solr</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Spring/">
                        <span class="tag">Spring</span>
                        <span class="tag is-grey">25</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/SpringBoot/">
                        <span class="tag">SpringBoot</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Vue/">
                        <span class="tag">Vue</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/WebSocket/">
                        <span class="tag">WebSocket</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/Yaml/">
                        <span class="tag">Yaml</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/css/">
                        <span class="tag">css</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/ffmpeg/">
                        <span class="tag">ffmpeg</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/gitalk/">
                        <span class="tag">gitalk</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/hexo/">
                        <span class="tag">hexo</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/jdk/">
                        <span class="tag">jdk</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/logger/">
                        <span class="tag">logger</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/markdown/">
                        <span class="tag">markdown</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/python/">
                        <span class="tag">python</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/time/">
                        <span class="tag">time</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/乱码/">
                        <span class="tag">乱码</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/二维码/">
                        <span class="tag">二维码</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/分库分表/">
                        <span class="tag">分库分表</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/反射/">
                        <span class="tag">反射</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/工具/">
                        <span class="tag">工具</span>
                        <span class="tag is-grey">5</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/并发/">
                        <span class="tag">并发</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/序列化/">
                        <span class="tag">序列化</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/手记/">
                        <span class="tag">手记</span>
                        <span class="tag is-grey">6</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/技术方案/">
                        <span class="tag">技术方案</span>
                        <span class="tag is-grey">22</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/指南/">
                        <span class="tag">指南</span>
                        <span class="tag is-grey">6</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/教程/">
                        <span class="tag">教程</span>
                        <span class="tag is-grey">20</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/方案设计/">
                        <span class="tag">方案设计</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/时区/">
                        <span class="tag">时区</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/时间窗口/">
                        <span class="tag">时间窗口</span>
                        <span class="tag is-grey">3</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/爬虫/">
                        <span class="tag">爬虫</span>
                        <span class="tag is-grey">5</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/问题记录/">
                        <span class="tag">问题记录</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
                <div class="control">
                    <a class="tags has-addons" href="/hexblog/tags/随笔/">
                        <span class="tag">随笔</span>
                        <span class="tag is-grey">1</span>
                    </a>
                </div>
                
            </div>
        </div>
    </div>
</div>
    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
            
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                分类
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/hexblog/categories/DB/">
            <span class="level-start">
                <span class="level-item">DB</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">41</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/InfluxDB/">
            <span class="level-start">
                <span class="level-item">InfluxDB</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">16</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/Mongo/">
            <span class="level-start">
                <span class="level-item">Mongo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">11</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/Mysql/">
            <span class="level-start">
                <span class="level-item">Mysql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">13</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/分库分表/">
            <span class="level-start">
                <span class="level-item">分库分表</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/">
            <span class="level-start">
                <span class="level-item">Java</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">52</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Agent/">
            <span class="level-start">
                <span class="level-item">Agent</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Android/">
            <span class="level-start">
                <span class="level-item">Android</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Bugfix/">
            <span class="level-start">
                <span class="level-item">Bugfix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/IO/">
            <span class="level-start">
                <span class="level-item">IO</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JDK/">
            <span class="level-start">
                <span class="level-item">JDK</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">22</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JVM/">
            <span class="level-start">
                <span class="level-item">JVM</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JavaWeb/">
            <span class="level-start">
                <span class="level-item">JavaWeb</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/其他/">
            <span class="level-start">
                <span class="level-item">其他</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/问题记录/">
            <span class="level-start">
                <span class="level-item">问题记录</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/">
            <span class="level-start">
                <span class="level-item">Python</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">33</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/Mongo/">
            <span class="level-start">
                <span class="level-item">Mongo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/MySql/">
            <span class="level-start">
                <span class="level-item">MySql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/教程/">
            <span class="level-start">
                <span class="level-item">教程</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">25</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/采坑记录/">
            <span class="level-start">
                <span class="level-item">采坑记录</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/项目/">
            <span class="level-start">
                <span class="level-item">项目</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/">
            <span class="level-start">
                <span class="level-item">Quick系列</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">30</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickAlarm/">
            <span class="level-start">
                <span class="level-item">QuickAlarm</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickCrawler/">
            <span class="level-start">
                <span class="level-item">QuickCrawler</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickFix/">
            <span class="level-start">
                <span class="level-item">QuickFix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickMedia/">
            <span class="level-start">
                <span class="level-item">QuickMedia</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickSpi/">
            <span class="level-start">
                <span class="level-item">QuickSpi</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickTask/">
            <span class="level-start">
                <span class="level-item">QuickTask</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/">
            <span class="level-start">
                <span class="level-item">Shell</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">39</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/CMD/">
            <span class="level-start">
                <span class="level-item">CMD</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">18</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Docker/">
            <span class="level-start">
                <span class="level-item">Docker</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Git/">
            <span class="level-start">
                <span class="level-item">Git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Maven/">
            <span class="level-start">
                <span class="level-item">Maven</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Ngins/">
            <span class="level-start">
                <span class="level-item">Ngins</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Nginx/">
            <span class="level-start">
                <span class="level-item">Nginx</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/环境搭建/">
            <span class="level-start">
                <span class="level-item">环境搭建</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/">
            <span class="level-start">
                <span class="level-item">前端</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Chrome/">
            <span class="level-start">
                <span class="level-item">Chrome</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Css/">
            <span class="level-start">
                <span class="level-item">Css</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Jquery/">
            <span class="level-start">
                <span class="level-item">Jquery</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/ReactJS/">
            <span class="level-start">
                <span class="level-item">ReactJS</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Vue/">
            <span class="level-start">
                <span class="level-item">Vue</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/">
            <span class="level-start">
                <span class="level-item">工具</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/工具类/">
            <span class="level-start">
                <span class="level-item">工具类</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/插件系列/">
            <span class="level-start">
                <span class="level-item">插件系列</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/">
            <span class="level-start">
                <span class="level-item">开源</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">46</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Guava/">
            <span class="level-start">
                <span class="level-item">Guava</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Hexo/">
            <span class="level-start">
                <span class="level-item">Hexo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Hystrix/">
            <span class="level-start">
                <span class="level-item">Hystrix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Mybatis/">
            <span class="level-start">
                <span class="level-item">Mybatis</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/OGNL/">
            <span class="level-start">
                <span class="level-item">OGNL</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/RabbitMQ/">
            <span class="level-start">
                <span class="level-item">RabbitMQ</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Redis/">
            <span class="level-start">
                <span class="level-item">Redis</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Solr/">
            <span class="level-start">
                <span class="level-item">Solr</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Spring/">
            <span class="level-start">
                <span class="level-item">Spring</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">24</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Yaml/">
            <span class="level-start">
                <span class="level-item">Yaml</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/火花/">
            <span class="level-start">
                <span class="level-item">火花</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/">
            <span class="level-start">
                <span class="level-item">随笔</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">7</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/idea/">
            <span class="level-start">
                <span class="level-item">idea</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/吐槽/">
            <span class="level-start">
                <span class="level-item">吐槽</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li>
            </ul>
        </div>
    </div>
</div>
        
            <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">14</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/01/">
                <span class="level-start">
                    <span class="level-item">一月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/11/">
                <span class="level-start">
                    <span class="level-item">十一月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">6</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/10/">
                <span class="level-start">
                    <span class="level-item">十月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/09/">
                <span class="level-start">
                    <span class="level-item">九月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/08/">
                <span class="level-start">
                    <span class="level-item">八月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/07/">
                <span class="level-start">
                    <span class="level-item">七月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/06/">
                <span class="level-start">
                    <span class="level-item">六月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/05/">
                <span class="level-start">
                    <span class="level-item">五月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">12</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/04/">
                <span class="level-start">
                    <span class="level-item">四月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/03/">
                <span class="level-start">
                    <span class="level-item">三月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/02/">
                <span class="level-start">
                    <span class="level-item">二月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/01/">
                <span class="level-start">
                    <span class="level-item">一月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">14</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">8</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/11/">
                <span class="level-start">
                    <span class="level-item">十一月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">10</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/10/">
                <span class="level-start">
                    <span class="level-item">十月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/09/">
                <span class="level-start">
                    <span class="level-item">九月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">13</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/08/">
                <span class="level-start">
                    <span class="level-item">八月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/07/">
                <span class="level-start">
                    <span class="level-item">七月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">23</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/06/">
                <span class="level-start">
                    <span class="level-item">六月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">22</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/05/">
                <span class="level-start">
                    <span class="level-item">五月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/04/">
                <span class="level-start">
                    <span class="level-item">四月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/03/">
                <span class="level-start">
                    <span class="level-item">三月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">16</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/02/">
                <span class="level-start">
                    <span class="level-item">二月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">10</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/01/">
                <span class="level-start">
                    <span class="level-item">一月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">13</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">6</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/09/">
                <span class="level-start">
                    <span class="level-item">九月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/08/">
                <span class="level-start">
                    <span class="level-item">八月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/07/">
                <span class="level-start">
                    <span class="level-item">七月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/06/">
                <span class="level-start">
                    <span class="level-item">六月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/05/">
                <span class="level-start">
                    <span class="level-item">五月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
        
        </div>
    
</div>

                




<div class="column is-4-tablet is-4-desktop is-2-widescreen is-hidden-touch is-hidden-desktop-only has-order-3 column-right ">
    
        
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                分类
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/hexblog/categories/DB/">
            <span class="level-start">
                <span class="level-item">DB</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">41</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/InfluxDB/">
            <span class="level-start">
                <span class="level-item">InfluxDB</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">16</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/Mongo/">
            <span class="level-start">
                <span class="level-item">Mongo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">11</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/Mysql/">
            <span class="level-start">
                <span class="level-item">Mysql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">13</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/DB/分库分表/">
            <span class="level-start">
                <span class="level-item">分库分表</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/">
            <span class="level-start">
                <span class="level-item">Java</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">52</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Agent/">
            <span class="level-start">
                <span class="level-item">Agent</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Android/">
            <span class="level-start">
                <span class="level-item">Android</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/Bugfix/">
            <span class="level-start">
                <span class="level-item">Bugfix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/IO/">
            <span class="level-start">
                <span class="level-item">IO</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JDK/">
            <span class="level-start">
                <span class="level-item">JDK</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">22</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JVM/">
            <span class="level-start">
                <span class="level-item">JVM</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/JavaWeb/">
            <span class="level-start">
                <span class="level-item">JavaWeb</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/其他/">
            <span class="level-start">
                <span class="level-item">其他</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Java/问题记录/">
            <span class="level-start">
                <span class="level-item">问题记录</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/">
            <span class="level-start">
                <span class="level-item">Python</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">33</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/Mongo/">
            <span class="level-start">
                <span class="level-item">Mongo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/MySql/">
            <span class="level-start">
                <span class="level-item">MySql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/教程/">
            <span class="level-start">
                <span class="level-item">教程</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">25</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/采坑记录/">
            <span class="level-start">
                <span class="level-item">采坑记录</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Python/项目/">
            <span class="level-start">
                <span class="level-item">项目</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/">
            <span class="level-start">
                <span class="level-item">Quick系列</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">30</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickAlarm/">
            <span class="level-start">
                <span class="level-item">QuickAlarm</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickCrawler/">
            <span class="level-start">
                <span class="level-item">QuickCrawler</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickFix/">
            <span class="level-start">
                <span class="level-item">QuickFix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickMedia/">
            <span class="level-start">
                <span class="level-item">QuickMedia</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickSpi/">
            <span class="level-start">
                <span class="level-item">QuickSpi</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Quick系列/QuickTask/">
            <span class="level-start">
                <span class="level-item">QuickTask</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/">
            <span class="level-start">
                <span class="level-item">Shell</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">39</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/CMD/">
            <span class="level-start">
                <span class="level-item">CMD</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">18</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Docker/">
            <span class="level-start">
                <span class="level-item">Docker</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Git/">
            <span class="level-start">
                <span class="level-item">Git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Maven/">
            <span class="level-start">
                <span class="level-item">Maven</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Ngins/">
            <span class="level-start">
                <span class="level-item">Ngins</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/Nginx/">
            <span class="level-start">
                <span class="level-item">Nginx</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/Shell/环境搭建/">
            <span class="level-start">
                <span class="level-item">环境搭建</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/">
            <span class="level-start">
                <span class="level-item">前端</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Chrome/">
            <span class="level-start">
                <span class="level-item">Chrome</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Css/">
            <span class="level-start">
                <span class="level-item">Css</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Jquery/">
            <span class="level-start">
                <span class="level-item">Jquery</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/ReactJS/">
            <span class="level-start">
                <span class="level-item">ReactJS</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/前端/Vue/">
            <span class="level-start">
                <span class="level-item">Vue</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/">
            <span class="level-start">
                <span class="level-item">工具</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">9</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/工具类/">
            <span class="level-start">
                <span class="level-item">工具类</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/工具/插件系列/">
            <span class="level-start">
                <span class="level-item">插件系列</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/">
            <span class="level-start">
                <span class="level-item">开源</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">46</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Guava/">
            <span class="level-start">
                <span class="level-item">Guava</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Hexo/">
            <span class="level-start">
                <span class="level-item">Hexo</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Hystrix/">
            <span class="level-start">
                <span class="level-item">Hystrix</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Mybatis/">
            <span class="level-start">
                <span class="level-item">Mybatis</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/OGNL/">
            <span class="level-start">
                <span class="level-item">OGNL</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/RabbitMQ/">
            <span class="level-start">
                <span class="level-item">RabbitMQ</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Redis/">
            <span class="level-start">
                <span class="level-item">Redis</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Solr/">
            <span class="level-start">
                <span class="level-item">Solr</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Spring/">
            <span class="level-start">
                <span class="level-item">Spring</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">24</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/开源/Yaml/">
            <span class="level-start">
                <span class="level-item">Yaml</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li><li>
        <a class="level is-marginless" href="/hexblog/categories/火花/">
            <span class="level-start">
                <span class="level-item">火花</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/">
            <span class="level-start">
                <span class="level-item">随笔</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">7</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/idea/">
            <span class="level-start">
                <span class="level-item">idea</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/hexblog/categories/随笔/吐槽/">
            <span class="level-start">
                <span class="level-item">吐槽</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li></ul></li>
            </ul>
        </div>
    </div>
</div>
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">14</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2020/01/">
                <span class="level-start">
                    <span class="level-item">一月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/11/">
                <span class="level-start">
                    <span class="level-item">十一月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">6</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/10/">
                <span class="level-start">
                    <span class="level-item">十月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/09/">
                <span class="level-start">
                    <span class="level-item">九月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/08/">
                <span class="level-start">
                    <span class="level-item">八月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/07/">
                <span class="level-start">
                    <span class="level-item">七月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/06/">
                <span class="level-start">
                    <span class="level-item">六月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/05/">
                <span class="level-start">
                    <span class="level-item">五月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">12</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/04/">
                <span class="level-start">
                    <span class="level-item">四月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/03/">
                <span class="level-start">
                    <span class="level-item">三月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">7</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/02/">
                <span class="level-start">
                    <span class="level-item">二月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2019/01/">
                <span class="level-start">
                    <span class="level-item">一月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">14</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">8</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/11/">
                <span class="level-start">
                    <span class="level-item">十一月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">10</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/10/">
                <span class="level-start">
                    <span class="level-item">十月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/09/">
                <span class="level-start">
                    <span class="level-item">九月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">13</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/08/">
                <span class="level-start">
                    <span class="level-item">八月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/07/">
                <span class="level-start">
                    <span class="level-item">七月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">23</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/06/">
                <span class="level-start">
                    <span class="level-item">六月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">22</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/05/">
                <span class="level-start">
                    <span class="level-item">五月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/04/">
                <span class="level-start">
                    <span class="level-item">四月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">11</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/03/">
                <span class="level-start">
                    <span class="level-item">三月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">16</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/02/">
                <span class="level-start">
                    <span class="level-item">二月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">10</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2018/01/">
                <span class="level-start">
                    <span class="level-item">一月 2018</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">13</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/12/">
                <span class="level-start">
                    <span class="level-item">十二月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">6</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/09/">
                <span class="level-start">
                    <span class="level-item">九月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/08/">
                <span class="level-start">
                    <span class="level-item">八月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/07/">
                <span class="level-start">
                    <span class="level-item">七月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/06/">
                <span class="level-start">
                    <span class="level-item">六月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/hexblog/archives/2017/05/">
                <span class="level-start">
                    <span class="level-item">五月 2017</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
    
    
</div>

            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/hexblog/">
                
                    <img src="/hexblog/images/avatar.jpg" alt="一灰灰Blog" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 YiHui&nbsp;
                Powered by <a href="https://hexo.io/" target="_blank">Hexo</a> & <a
                        href="https://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="Download on GitHub" href="https://github.com/liuyueyi">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="Chart in Weibo" href="https://weibo.com/p/1005052169825577/home">
                        
                        <i class="fab fa-weibo"></i>
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="Frends with me" href="https://s10.mogucdn.com/mlcdn/c45406/171229_1cgld3igbelkbc70cd8af1j3809kb_150x150.jpg">
                        
                        <i class="fab fa-weixin"></i>
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
    <hr/>
    <div id="foot-pannel">
        <div class="outer">
            <div class="foot-column">
                <!--关注我们-->
                <div class="widget-wrap widget-list">
                    <h3 class="widget-title">更多平台</h3>
                    <div class="widget">
                        <ul>
                            <li>
                                <a href="//blog.hhui.top/">一灰灰Blog</a>
                            </li>
                        
                            <li>
                                <a href="//juejin.im/user/5a2a4b095188252ae93adbbf/posts">掘金</a>
                            </li>
                        
                            <li>
                                <a href="//my.oschina.net/u/566591">开源中国</a>
                            </li>
                        
                            <li>
                                <a href="//blog.csdn.net/liuyueyi25">CSDN</a>
                            </li>
                        
                            <li>
                                <a href="//www.jianshu.com/u/5902ab08e670">简书</a>
                            </li>
                        
                            <li>
                                <a href="//cloud.tencent.com/developer/column/1847">云+</a>
                            </li>
                        
                            <li>
                                <a href="//www.toutiao.com/c/user/69862071663/#mid=1579653107239950">头条</a>
                            </li>
                        
                            <li>
                                <a href="//github.com/liuyueyi">GitHub</a>
                            </li>
                        
                            <li>
                                <a href="//gitee.com/liuyueyi">Gitee</a>
                            </li>
                            
                        </ul>
                    </div>
                </div>
            </div>
            <div class="foot-column">
                <!--联系合作-->
                <div class="widget-wrap widget-list">
                    <h3 class="widget-title">一灰灰Blog</h3>
                    <div class="widget">
                        <ul>
                            
                                <li>
                                    <a href="#">QQ : 3302797840</a>
                                </li>
                            
                                <li>
                                    <a href="#">微信 : liuyueyi25</a>
                                </li>
                            
                                <li>
                                    <a href="#">邮箱 : bangzewu@126.com</a>
                                </li>
                            
                                <li>
                                    <a href="#">微博 : 一灰灰blog</a>
                                </li>
                            
                        </ul>
                    </div>
                </div>
                <div class="widget-wrap widget-list">
                    <h3 class="widget-title">友情链接</h3>
                    <div class="widget">
                        <ul>
                            
                                <span class="label">
                                    <a target="_blank" href="//zweb.hhui.top">zweb多媒体工具页</a>
                                </span>
                            
                                <span class="label">
                                    <a target="_blank" href="//mweb.hhui.top">mweb古诗选</a>
                                </span>
                            
                        </ul>
                    </div>
                </div>
            </div>
            <div class="foot-column">
                <!--友情链接-->
                <div class="widget-wrap widget-list">
                    <h3 class="widget-title">知识星球</h3>
                    <div class="widget">
                        <img style="width: 200px;" src="/hexblog/imgs/info/xingqiu.png">
                    </div>
                </div>
                
            </div>
            <div class="foot-column">
                <div class="widget-wrap widget-list">
                    <h3 class="widget-title">公众号</h3>
                    <div class="widget">
                        <img style="width: 200px;" src="/hexblog/imgs/info/wx.jpg">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div style="padding-top:20em">
        <hr/>
        <div id="foot-info">
            <span style="margin:4px;font-size:1em">
                &copy; 2020 <a target='_blank' href='https://github.com/liuyueyi'>一灰灰Blog</a> 版权所有 | <a href="http://www.beian.miit.gov.cn" target="_blank">鄂ICP备18017282号</a>
            </span>
            <br/>
            <span style="margin:4px;font-size:1em">
                <script type="text/javascript">document.write(unescape("%3Cspan id='cnzz_stat_icon_1278691600'%3E%3C/span%3E%3Cscript src='https://s9.cnzz.com/z_stat.php%3Fid%3D1278691600%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
            </span>
            <span style="margin:1em;font-size:1.4em">
                <label id="self_count_cnt"><br>本站总访量: <span class="visit_cnt">69330</span> | 总访问人次: <span class="visit_cnt">11586</span> | 恭喜您为第 <span class="visit_cnt">10840</span> 访问者</label>
            </span>
            <script src="/hexblog/js/count.js"></script>
        </div>
    </div>
</footer>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("zh-CN");</script>


    
    
    
    <script src="/hexblog/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/hexblog/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="回到顶端" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/hexblog/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>
    <script src="/hexblog/js/clipboard.js" defer></script>
    

    
    
    


<script src="/hexblog/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="想要查找什么..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: '文章',
                PAGES: '页面',
                CATEGORIES: '分类',
                TAGS: '标签',
                UNTITLED: '(无标题)',
            },
            CONTENT_URL: '/hexblog/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/hexblog/js/insight.js" defer></script>
<link rel="stylesheet" href="/hexblog/css/search.css">
<link rel="stylesheet" href="/hexblog/css/insight.css">
    
</body>
</html>